I'm trying to implement a parent-child relationship between 3 objects, and I want to constrain what can be a parent of what. interface3's parent can only be interface2; interface2's parent can be either interface3 or interface2
interface3 -> interface2 -> interface3 -> interface2 -> ... -> interface2 -> interface1
As a first step, I tried to implement an interface hiearchy that only allows
interface3 -> interface2 -> interface1
I thought the following interface definitions would do it, but the compiler complains that The type 'interfaces.Interface2' must be convertible to 'interfaces.GenericInterface<interfaces.Interface2>' in order to use it as parameter 'P' in the generic type or method 'interfaces.GenericInterface<P>'
public interface GenericInterface<P> where P : GenericInterface<P>
{
P getParent();
void setParent(P parent);
}
public interface Interface1 : GenericInterface<Interface1>
{
}
public interface Interface2 : GenericInterface<Interface1>
{
}
public interface Interface3 : GenericInterface<Interface2>
{
}
The above interfaces compile if I don't use the where clause, but with the where clause I get the error mentioned above. Did I do something wrong, or will this never work (without getting rid of the where clause)

Recursive Hierarchy of generic interfaces
R.Tutus
Hi,
I don't clearly underestand what you are trying to do. Can you explain the relationships better
Your code will only work if each interface is a GenericIntereface of itself.
To make it compile (though I'm not sure if that's enough to make it work). Change the declaration of interface2 into
public interface Interface2 : GenericInterface<Interface1>, GenericInterface<Interface2>
{
}
Allen Razdow
I guess why I'd like to do is something like this
public
interface GenericInterface<P> where P : GenericInterfaceI want each 'node' in the tree to be a type of GenericInterface (regardless of the parameter P). But the above doesn't work; I need to specify a type for GenericInterface in the where clause (hence: public interface GenericInterface<P> where P : GenericInterface<P>)
The only reason I want to use GenericInterface<P> (as opposed to a non-generic base interface, like INode) is so that getParent/setParent use the right type, avoiding a typecast.