Berryl Hesh wrote:
Converting in the other direction , IEnumerable<Interfaceto
List<ImplInterface>, I can just create a new List<ImplInterface(data)
where data is IEnumerable<Interface>.
How can I convert the List<ImplInterfaceto IEnumerable<Interface>?
If I understand the question correctly, you have a class ImplInterface that
implements Interface. It's actually unclear to me how you would be able to
convert a generic IEnumerable<Interfaceto a List<ImplInterface>: the
Interface elements in the enumerable don't need to be of type ImplInterface.
Is ImplInterface a wrapper type?
The other way around is more obvious, type-wise. If you have C# 3.0 and .NET
3.5, it's as easy as
aListImplInterface.Cast<Interface>()
If you don't, you can write this method yourself:
static class EnumerableConversion {
static IEnumerable<TCast<T, U>(IEnumerable<Usource) where U : T {
foreach (U u in source) yield return (T) u;
}
}
And call it like so:
EnumerableConversion.Cast<Interface, ImplInterface>(aListImplInterface)
Not half as snappy, obviously.
--
J.