tags:

views:

47

answers:

2

I've got these interfaces:

alt text

And this code:

IList<IFinder<IDomainObject>> finders = new List<IFinder<IDomainObject>>();
IFinder<IOrder> orderFinder = this.mocks.StrictMock<IFinder<IOrder>>();
finders.Add(orderFinder);

I get this 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?

+3  A: 

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.

Marcelo Cantos
Yes I've done this in .NET 3.5 and VS 2008.But I just tried updating it to VS 2010 and the projects to .NET 4 and I'm still getting the same error.
Paw Baltzersen
... but that might be because IList or my own IFinder doesn't support covariance?
Paw Baltzersen
@Paw: You have to add the `out` specifier to your interface's generic type parameter.
Adam Robinson
@Adam: Thank you :) It works now.Now I just need to make my team upgrade to VS 2010 ASAP ;) Shouldn't be that big a problem.
Paw Baltzersen
+1  A: 

Covariance and Contravariance in generics are new .Net 4.0 features i think. Good read: http://msdn.microsoft.com/en-us/library/dd799517.aspx

MrDosu