如何映射名为 Unit 的 ac# 类,该类又具有List<Unit>
.
具体场景是一个 rootUnit 对象,其中包含一个作为第一级子级的 List。
第一级子单元对象将不包含任何其他单元,因此层次结构中不会有递归。
public class Unit
{
public Unit()
{
// Explicitly set the default value for the first unit in a hierarchy
HierarchyIndex = 0;
Units = new List<Unit>();
}
public List<Unit> Units { get; set; }
public int UnitId { get; set; }
public string Name { get; set; }
public Nullable<int> ParentId { get; set; }
public int TemplateId { get; set; }
public bool HasChildren { get; set; }
public bool IsFolder { get; set; }
public DateTime CreatedAt { get; set; }
public int HierarchyIndex { get; set; }
}
将上面的单元映射到这个视图模型:
public class UnitTreeViewModel
{
[JsonProperty("key")]
public int UnitId { get; set; }
[JsonProperty("title")]
public string Name { get; set; }
[JsonProperty("isLazy")]
public bool HasChildren { get; set; }
[JsonProperty("isFolder")]
public bool IsFolder { get; set; }
}