I have defined this method:
public static List<T2> ConvertList<T1, T2>(List<T1> param) where T1:class where T2:class { List<T2> result = new List<T2>(); foreach (T1 p in param) result.Add((T2)p); return result; }
To convert type 1 lists to type 2 lists.
Unfortunately, I forgot that the C # compiler cannot say at this stage that T1 converting to T2 , so it throws an error:
error CS0030: cannot convert type T1 to T2
Can someone guide me how to do it right? I need this method only to convert a list of a custom class to an object list, since in .NET everything comes from object , it should work.
Basically, I would like some kind of syntax to tell the compiler that T2 ( object ) is the base for T1 ( MyClass ), so something like:
public static List<T2> ConvertList<T1, T2>(List<T1> param) where T2: base of T1
(... , where T2: base T1 )
Jerry Switalski
source share