Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

c# - Generics and Parent/Child architecture

I'm building an architecture with inheritable generics and parent-children relations. I have one major problem: I can't make both the child and the parent aware of each other's type, only one of the two.

I need both the child and the parent to be aware of each other's type.

Scenario 1: Parent knows child type, but child only knows generic parent with generic children.

public class Child
{
    public Parent<Child> Parent;
}

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

Scenario 2: Child knows parent type, but parent only knows generic children with generic parent.

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

public class Parent
{
    public List<Child<Parent>> Children;
}

Scenario 3: The utopic but unachievable 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 won't compile, because Parent and Child take a second generic type that would be their own type, but I can't (or at least don't know how!) to specify it is their own type.

I'm falling in some kind of a infinite loop/recursion/ball-throw here, please help me before I drown.

Good luck.

EDIT: If I wasn't clear, I can reference my types yes, but if the user derives Child into FooChild, and accesses the children of the parent of FooChild, these will only be Child, and not FooChild, as it should be.

Failure Example with Scenario 1:

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

Failure Example with Scenario 2:

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!
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You certainly can do what you ask. Define your classes like this:

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 concrete class like this:

public class Cat : Parent<Cat, Kitten>
{
}

public class Kitten : Child<Cat, Kitten>
{
}

public class Dog : Parent<Dog, Puppy>
{
}

public class Puppy : Child<Dog, Puppy>
{
}

Now you have your fully-typed parent-child relationships. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...