开发者

Adding a derived interface to generic interface

I've got these interfaces:

Adding a derived interface to generic interface

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜