In my c# project, I have two abstract generic parent classes which then have multiple child classes that inherit them. The problem is that I am passing the child type to the parent generic, and its making for some silly looking class and method definitions.
Is there a way in the Generic parent classes to get the type of a class inheriting them? So instead of having to pass type in DTOBase, declare that will be the type of the first child of DTOBase.
Parent Class Definitions
public abstract class DTOBase<T> : INotifyPropertyChanged, IMergeable<T>
public abstract class ModelBase<T,U> : INotifyPropertyChanged, ITrackable, IMergeable<U>
where T : DTOBase<T>
where U : ModelBase<T,U>
Example Child Classes
public class MenuGroup : DTOBase<MenuGroup>
public class MenuGroup : ModelBase<DTOs.MenuGroup, MenuGroup>
Nope, that sort of thing is reasonably common when you have a type relationship which can’t easily be expressed any other way. I have something similar in my Protocol Buffers port:
It’s annoying and ugly, but it gets the job done. Sorry not to have better news for you.