Generics and Parent / Child Architecture

I am building an architecture with heritable generics and relationships between parents and children. I have one serious problem: I cannot make both parents and parents know about each other, only one of two.

I need both the child and the parent to be aware of every other type.

Scenario 1 : the parent knows the type of the child, but the child only knows the parent movie with the birth children.

public class Child { public Parent<Child> Parent; } public class Parent<TChild> where TChild : Child { public List<TChild> Children; } 

Scenario 2 : the child knows the parent type, but the parent only knows the common children with the birth parent.

 public class Child<TParent> where TParent : Parent { public TParent Parent; } public class Parent { public List<Child<Parent>> Children; } 

Scenario 3 : a utopian but unattainable scenario:

 public class Child<TParent> where TParent : Parent { public TParent Parent; } public class Parent<TChild> where TChild : Child { public List<TChild> Children; } 

Of course, scenario 3 will not compile, because the parent and child take the second type of the generic type, which will be their own type, but I cannot (or at least do not know how!) Indicate that it is their own type.

I fall into some kind of infinite loop / recursion / ball, please help me before drowning.

Good luck.

EDIT . If I was not clear, I can refer to my yes types, but if the user displays Child in FooChild and accesses the parents of FooChild, it will only be Child, not FooChild, as it should be.

Scenario 1 crash example :

 public class Child { public Parent<Child> Parent; } public class Parent<TChild> where TChild : Child, new() { public List<TChild> Children; } public class FooChild : Child { public int Required; public void Bar() { foreach (Child child in this.Parent.Children) { int x = child.Required; // cannot be accessed! } } } 

Scenario 2 crash example :

 public class Child<TParent> where TParent : Parent { public TParent Parent; } public class Parent { public List<Child<Parent>> Children; } public class FooChild : Child<Parent> { public int Required; public void Bar() { foreach (Child<Parent> child in this.Parent.Children) { int x = child.Required; // cannot be accessed! } } } 
+6
generics c # parent-child
source share
1 answer

You can certainly do what you ask. Define your classes as follows:

 public abstract class Child<P, C> where P : Parent<P, C> where C : Child<P, C> { public P Parent { get; set; } } public abstract class Parent<P, C> where P : Parent<P, C> where C : Child<P, C> { public List<C> Children = new List<C>(); } 

Then you can define your specific class as follows:

 public class Cat : Parent<Cat, Kitten> { } public class Kitten : Child<Cat, Kitten> { } public class Dog : Parent<Dog, Puppy> { } public class Puppy : Child<Dog, Puppy> { } 

You now have your fully typed parent-child relationship. This code now works:

 var fluffy = new Cat(); var fluffette1 = new Kitten() { Parent = fluffy }; var fluffette2 = new Kitten() { Parent = fluffy }; fluffy.Children.Add(fluffette1); fluffy.Children.Add(fluffette2); var butch = new Dog(); var butchette1 = new Puppy() { Parent = butch }; var butchette2 = new Puppy() { Parent = butch }; butch.Children.Add(butchette1); butch.Children.Add(butchette2); 

Hope this helps.

+15
source share

All Articles