The project I’m currently working on requires me to create a tree data structure.
Below is the sample how I’ve tried to achieve this functionality. I’ve decided to create a child node collection as a nested class since it allows me to set Nodes parent in it’s Add() method while keeping parent setter private, so that classes derived from Node or other classes in the same assembly couldn’t get direct access to it.
class Node<T> where T : Node<T>
{
private T mParent;
private ChildNodeCollection<T> mChildren;
public T Parent
{
get{return this.InnerParent;}
}
private T InnerParent
{
get{return this.mParent;}
set {this.mParent = value;}
}
public Node()
{
this.mChildren = new ChildNodeCollection<T>(this);
}
class ChildNodeCollection<U> where U : T
{
private U mParent;
public U CollectionParent
{
get{return this.mParent;}
}
public ChildNodeCollection(U parent)
{
this.mParent = parent;
}
public void Add(U item)
{
item.InnerParent = this.CollectionParent;
...
}
}
}
This code doesn’t compile though. It complains about this.mChildren = new ChildNodeCollection(this) line in the Node constructor. It throws these two errors.
Error 35 The best overloaded method match for Node<T>.ChildNodeColllection<T>.ChildNodeColllection(T)' has some invalid arguments
Error 36 Argument '1': cannot convert from Node<T> to T
I guess it can’t work out that T is Node, even though I specified so in the class definition. I’m curious if anybody has any ideas how this could be done differently in a way that would allow me to set Node’s parent when adding it to collection without exposing Node’s Parent property too much with internal access modifier.
In any case, you need to explicitly specify the type argument when creating a generic object using constructor, so you need to write something like:
This won’t work because the type of
thisisNode<T>and notT(which is what the constructor requires). I think the easiest way to fix it is to store the parent asNode<T>instead of using a generic parameter.The relevant parts of code would look like this:
I guess that your original goal (with using the
T : Node<T>constraing) was to use inheritance to define more specific types of nodes. Then you’d like to retreive children (or parents) statically typed asT(that is, your specific node type). I may be wrong, but I seriously doubt this can be expressed with .NET generics.I think it is a lot easier to use
Node<T>as a type representing a node that contains your value of typeTinstead of using inheritance.