Adding a derived interface to generic interface
I've got these interfaces:
And this code:
IList<IFinder<IDomainObject>> finders = new List<IFinder<IDomainObject>>();
IFinder<IOrder> orderFinder = this.mocks.StrictMock<IFinder<IOrder>>();
finders.Add(orderFinder);
I get th开发者_StackOverflow社区is error on the third line:
Argument '1': cannot convert from 'Mes.FrameworkTest.Repository.Finders.IFinder
<Mes.FrameworkTest.DomainModel.IOrder
>' to 'Mes.FrameworkTest.Repository.Finders.IFinder<Mes.FrameworkTest.DomainModel.IDomainObject
>'
Why do I get this error when IOrder is derived from IDomainObject?
This requires .Net 4's support for covariance, which would be expressed thus:
interface IFinder<out T> ...
If you don't have .Net 4 (VS 2010), you're out of luck.
Covariance and Contravariance in generics are new .Net 4.0 features i think. Good read: http://msdn.microsoft.com/en-us/library/dd799517.aspx
精彩评论