我想为分层结构化对象创建一个抽象类。
以下是我已经使用的内容,但现在我想将其设为通用
public class EventBase
{
private EventBase _Parent;
virtual public EventBase Parent
{
get
{
return _Parent;
}
set
{
_Parent = value;
}
}
[ForeignKey("Parent")]
private ICustomList<EventBase> _ChildList = new CustomList<EventBase>();
virtual public ICustomList<EventBase> ChildList
{
get
{
return _ChildList;
}
set
{
_ChildList = value;
}
}
}
类似的东西:
public class EventBaseGeneric
{
private GenericTypeThatIsSetInTheInheritingClass _Parent;
virtual public GenericTypeThatIsSetInTheInheritingClass Parent
{
get
{
return _Parent;
}
set
{
_Parent = value;
}
}
[ForeignKey("Parent")]
private ICustomList<GenericTypeThatIsSetInTheInheritingClass> _ChildList = new CustomList<GenericTypeThatIsSetInTheInheritingClass>();
virtual public ICustomList<GenericTypeThatIsSetInTheInheritingClass> ChildList
{
get
{
return _ChildList;
}
set
{
_ChildList = value;
}
}
}
非常感谢你如何解决这个问题!
祝你好运, Fabianus
答案 0 :(得分:0)
它看起来如下:
// T generic parameter must inherit EventBase<T>
public class EventBase<T>
where T : EventBase<T>
{
public virtual T Parent { get; set; }
[ForeignKey("Parent")]
public virtual ICustomList<T> ChildList { get; set; } = new CustomList<T>()
}
答案 1 :(得分:0)
我找到了答案:
public abstract class PersistentObjectBaseWithNameHierarchical <T>
{
private T _Parent;
virtual public T Parent
{
get
{
return _Parent;
}
set
{
_Parent = value;
UpdatePropertiesInHierachy();
}
}
[ForeignKey("Parent")]
private ICustomList<T> _ChildList = new CustomList<T>();
virtual public ICustomList<T> ChildList
{
get
{
return _ChildList;
}
set
{
_ChildList = value;
UpdatePropertiesInHierachy();
}
}
答案 2 :(得分:0)
public abstract class PersistentObjectBaseWithNameHierarchical<T> : PersistentObjectBaseWithName where T : PersistentObjectBaseWithNameHierarchical<T>
{
private PersistentObjectBaseWithNameHierarchical<T> _Parent;
virtual public PersistentObjectBaseWithNameHierarchical<T> Parent
{
get
{
return _Parent;
}
set
{
_Parent = value;
UpdatePropertiesInHierachy();
}
}
[ForeignKey("Parent")]
private ICustomList<PersistentObjectBaseWithNameHierarchical<T>> _ChildList = new CustomList<PersistentObjectBaseWithNameHierarchical<T>>();
virtual public ICustomList<PersistentObjectBaseWithNameHierarchical<T>> ChildList
{
get
{
return _ChildList;
}
set
{
_ChildList = value;
UpdatePropertiesInHierachy();
}
}
public void AddChild(PersistentObjectBaseWithNameHierarchical<T> child)
{
if (ChildList.Count() != 0)
child.OrderPosition = ChildList.Max(e => e.OrderPosition) + 1;
ChildList.Add(child);
}
public void OrderChildList()
{
foreach (var e in ChildList)
{
e.OrderChildList();
}
ChildList.Sort((s1, s2) => s1.OrderPosition.CompareTo(s2.OrderPosition));
}
public int Level
{
get
{
if (Parent != null)
{
return Parent.Level + 1;
}
else
{
return 1;
}
}
}
private double _OrderPosition;
virtual public double OrderPosition
{
get
{
if (_OrderPosition == 0)
{
// We use the Id as OrderPosition to keep creation order by default
_OrderPosition = Id;
}
return _OrderPosition;
}
set
{
_OrderPosition = value;
Parent?.ChildList.Sort((s1, s2) => s1.OrderPosition.CompareTo(s2.OrderPosition));
UpdatePropertiesInHierachy();
}
}
public void UpdatePropertiesInHierachy()
{
PersistentObjectBaseWithNameHierarchical<T> r = GetRoot(this);
DuringUpdatePropertiesInHierachy();
}
因为否则我在这里收到错误:
GetRoot(本)
讲
参数1:无法从'HostSystems.Models.PersistentObjectBaseWithNameHierarchical'转换为'T'
感谢您提供进一步的建议!
此致 Fabianus