Casting a list of objects to their interface type in C#
.net, c#, generics
Solution
I answered the same question yesterday, although for base classes rather than interfaces.
The way to make this work is to iterate over the list and cast the elements. This can be done using ConvertAll:
IList<A> listOfA = new List<C>().ConvertAll(x => (A)x);
You could also use Linq:
IList<A> listOfA = new List<C>().Cast<A>().ToList();
Problem
I know I can cast an object from its own type to its interface type like so: ``` IMyInterface myValue = (IMyInterface)MyObjectThatImplementsMyInterface; ``` How can I cast `IList<MyClassThatImplementMyInterface>` to `IList<IMyInterface>`?