I have an interface:
public interface IMyInterface { }
I have a class which implements the interface:
public class MyClass : IM开发者_JS百科yInterface { }
I have a function which asks for an IEnumerable of IMyInterface:
private void TestFunction(IEnumerable<IMyInterface> collectie) { }
I can't figure out why this doesn't work:
ObservableCollection<MyClass> myClasses = new ObservableCollection<MyClass>();
TestFunction(myClasses); // this line doesn't work
An ObservableCollection is an IEnumerable, right?
And my class does implement, right?
So why doesn't this work?
It gives this 2 errors:
Error 1 The best overloaded method match for 'WindowsFormsApplication1.Form1.TestFunction(System.Collections.Generic.IEnumerable)' has some invalid arguments C:\spike\spike_rtf\WindowsFormsApplication1\Form1.cs 59 4 WindowsFormsApplication1 Error 2 Argument '1': cannot convert from 'System.Collections.ObjectModel.ObservableCollection' to 'System.Collections.Generic.IEnumerable' C:\spike\spike_rtf\WindowsFormsApplication1\Form1.cs 59 17 WindowsFormsApplication1
You're after generic variance. This will be supported in .NET 4.0 and C# 4.0, only for interfaces and delegates (and only where appropriate). That's okay for your case though as you're interested in the IEnumerable<T>
interface which becomes IEnumerable<out T>
in .NET to indicate its covariance.
The simplest way to do this in .NET 3.5 is probably to use Cast<>
:
TestFunction(myClasses.Cast<IMyInterface>());
(This is like User Friendly's "Select" suggestion, but a bit simpler. It's also worth knowing about for cases where it wouldn't work in C# 4 - if you happen to know that every item in a collection will be of the right type, but you can't express that in the type system.)
To find out (a lot) more about this, see Eric Lippert's blog posts on the topic. Be prepared for your mind to melt - I know mine does when I think about variance too hard. Eric has written various answers on this topic here on Stack Overflow too, including this one suggested in the comments.
This "feature" will be supported in .NET 4.0 http://msdn.microsoft.com/en-us/library/dd799517(VS.100).aspx
Use LINQ to convert from ObservableCollection to ObservableCollection:
var converted = myClassCollection.Select(o=>(IMyInterface)o).ToIEnumerable();
Its a well known limitations of generics in .NET. There's a super cool technical term for it, which I can't quite remember at this point.
Edit: generic variance. Damn you, Skeet!
You can use a Linq extension method Cast to do this:
class Program
{
static void Main(string[] args)
{
var original = new List<Bar>();
IEnumerable<IFoo> foos = original.Cast<IFoo>();
}
}
public interface IFoo { }
public class Bar : IFoo { }
精彩评论