I have a class
public class LDBList<T> : List<T> where T : LDBRootClass {
// typical constructor
public LDBList(LDBList<T> x) : base(x) { }
...
}
but I want to have an extra constructor that takes a list of a different generic type (say A), and a function that converts an A to a T, and build the T list from that, something like
public LDBList(
Func<A, T> converter,
IList<A> aList)
{
foreach (var x in aList) {
this.Append(converter(x));
}
}
so converter is of type A->T so I take an A list and make a T list from it. My class is parameterised by T so that's fine.
But it's complaining "The type or namespace name 'A' could not be found".
OK, so it needs the an A generic parameter on the class I suppose (it really doesn't like it on the constructor). But where do I put it, in fact is this even possible?
public void Foo<A, T>(), but since it's a constructor, and generic constructors are not supported, you need to have a way to provide the type. As you suggested, you could add a second generic parameter to the class definition (i.e.,public class LDBList<T, A>). Alternatively, you could use a static method that does the conversion and returns an object of typeLDBList<T>.