c# - 如何找到属性的深度?
问题描述
public class CategoryInModel
{
public int Id { get; set; }
public CategoryInModel Parent { get; set; }
}
我有一个像上面这样的课程。我想获得父对象的深度。
父对象可以有不同的深度。喜欢:
父.父.父
或者
父.父
如何找到父对象的深度?
解决方案
基于模型深度为 1 + 父深度的逻辑:
public class CategoryInModel
{
public int Id { get; set; }
public CategoryInModel Parent { get; set; }
public int Depth => 1 + ParentDepth;
public int ParentDepth => Parent?.Depth ?? 0;
}
推荐阅读
- r - 使线条图上的线条在背景中透明
- powershell - 如何获取-ADDefaultDomainPasswordPassword 区分名称?
- python - 如何将 gzip 压缩的 .tsv 文件插入 PostgreSQL?
- c# - 构造函数与初始化中的 dbContext
- php - 无法再阅读 Wordpress 网站
- hyperledger-fabric - Hyperledger Fabric CA:致命错误:运行时执行期间出现意外信号
- android - 我是否正确取消协程
- python-3.x - 基于固定参数函数调用的 Tkinter 级联菜单
- xml - 如何根据员工匹配从父节点复制子节点并将其保存在另一个父节点中?
- reactjs - 当我运行此代码时,“TypeError:无法读取未定义的属性‘映射’”