While I cannot offer a Java solution, here are some of them for C # ...
If you can change the process signature to accept IEnumerable ...
public void Process(IEnumerable<A> myList) { ... }
then everything will work in C # 4.0, thanks to improved support for companion and opposite variance.
If you work in C # 3.0, you can enter a generic parameter type in a method:
public void Process<T>(List<T> myList) where T : A { ... }
You can then invoke the transfer of a List or List, and the generic type parameter will be bound accordingly. Note that you do not often have to specify it directly, as the inferrence type usually gives you what you need.
If this doesn't work, you can convert the list using the Cast extension method from Enumerable:
public void Process(List<A> myList) { ... } var someList = getListA1(); Process( someList.Cast<A>());
Bevan
source share