I am trying to do the following and failing:
class base {}
class derived1 : base {}
class derived2 : base {}
interface itr<t>
where t : class, base
{}
class c1: itr<derived1>
{}
class c2 : itr<derived2>
{}
//The following 2 registrations fail:
_unityContainer.RegisterType<itr<base>, c1>("c1");
_unityContainer.RegisterType<itr<base>, c2>("c2");
The error I get is that the second parameter in the above registrations cannot be typecast into the first parameter and this registration is not valid. Any suggestions on how I can do this?
I need to do the above instead of registering with the derived1 or derived2 classes as generic parameters because while resolving I don't want to have to know the exact derived type I am resolving. I only want to work with base type methods polymorphically.
You cannot do this because generics are not covariant. That is, the type
itr<derived1>
cannot be converted toitr<base>
, even thoughderived1
can be converted tobase
.Here's an example of why, using the framework
List<T>
class:So accessing
list1[1]
would return a boxedint
in a list that was declared to containstring
s.Therefore, this cast is not allowed since it would break the type system.
(As a side note, in the clause
where t : class, base
, you do not need to specifyclass
.base
itself is a reference type, so theclass
constraint is redundant.)