I have an abstract base class
class AbstractClass
{
Col<AbstractClass> parent
public AbstractClass()
{
//do stuff
}
}
I have two implementations
class A : AbstractClass
{
Col<A> parent
public A(Col<A> parent)
:base(parent)
{
this.parent = parent;
}
}
class B : AbstractClass
{
Col<B> parent
public B(Col<B> parent)
:base(parent)
{
this.parent = parent;
}
}
I have a collection
class Col<T> : IList<T> where T : AbstractClass
Which is to be used in another class as Col<A> and Col<B>
, let's call this class C
.
class C
{
List<A> a = new List<A>()
List<B> b = new List<B>()
}
This would all work, except that I want types A
and B
to know about their parent collection. I thought having the following constructors in AbstractClass
, A, B would be ok, but it seems generic constraints are only available for classes and not on methods. Essentially I would like the following constructors:
public AbstractClass(Col<T> where T : AbstractClass)
public A(Col<A>)
public B(Col<B>)
instances of A, B need to know what collection they're in, but I can't call the base constructor from the derived classes because they're different types.
Help!