Fabianus Fabianus - 17 days ago 5
C# Question

generic abstract class for hierarchical structured

I would like to create an abstract class for hierarchical structured objects.

Here is what I already use, but now I want to make it generic

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;
}
}
}


something like this:

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;
}
}
}


Thanks a lot for any idea on how to achiv this !

Best regards,
Fabianus

Answer

It would look as follows:

// 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>()
}