1
Answer

c# generic type on where constraint

ora cle

ora cle

7y
436
1
Dear All.
 
I have below simple object model where Manager class consist List Of Child objects and Child object must have reference to it's parent object:
  1. public class ManagerBase<T1> where T1 : ChildBase<???>  
  2. {  
  3. public ManagerBase()  
  4. {  
  5. ChildObjects = new List<T1>();  
  6. }  
  7.   
  8. public List<T1> ChildObjects { getset; }  
  9. }  
  10.   
  11. public class ChildBase<T1> where T1 : ManagerBase<???>  
  12. {  
  13. public ChildBase(T1 parentMgr)  
  14. {  
  15. ParentMgr = parentMgr;  
  16. ParentMgr.ChildObjects.Add(this);  
  17. }  
  18.   
  19. public T1 ParentMgr { getset; }  
  20. }  
Above are BASE classes. Now, below are inherited sample Manager and Child classes. I don't know how to make below classes to compile as above BASE classes are not valid yet. Could you pls help? Thanks.
  1. public class CatalogManager : ManagerBase<Catalog>  
  2. {  
  3. }  
  4.   
  5. public class Catalog : ChildBase<CatalogManager>  
  6. {  
  7. }  
To provide more clear idea: I have BASE Manager class, BASE Child Object class. There are different type of inherited Managers (CatalogManager, DocumentManager etc.) and different type of Child Objects (Catalog, Document etc). Now, each Manager must consist of List of different type of child object not List of ChildBase. F.e: CatalogManager with List, DocumentManager with List. At the same time, each child object must have reference to it's Manager. In other words, I need strong typing instead of using Base classes. Hope it clear.
 
Thanks for your time.
Answers (1)