I have an interface with a contravariant type parameter like IFoo :
interface IFoo<in T> {}
Now I have three classes:
class B {} class D : B {} class Foo : IFoo<B> {}
If I register them this way
container.RegisterType<IFoo<B>, Foo>();
... then try to allow IFoo<D> , it fails because I really have not registered IFoo<D> or IFoo<> , This is clear.
My current solution is simply iterating through Registrations , find the registration in which RegisteredType can be assigned from an allow type ( IFoo<D> in my case), then enable its type MappedToType .
The question is simple: is there a better way to do this? Any advice would be appreciated.
Thanks.
EDIT:
A bit more context.
I have some kind of cartographer.
IMapper<in TSource, in TDest> : IMapper // IMapper is non-generic { void Map(TSource source, TDest dest); }
And I want to register only the base mapper (where TSource is IEnumerable<T> ) and be able to enable this handler for every type that implements IEnumerable<T> , for example, for T[] :
object source = GetSource(); // runtime type is T[] object dest = GetDest(); Type mapperType = typeof(IMapper<,>).MakeGenericType(source.GetType(), dest.GetType()); IMapper mapper = (IMapper) container.Resolve(mapperType); mapper.Map(source, dest);
And yes, I'm only interested in Unity / C # based approaches ...