I'm trying to register a open generic interface IService<T1>
and to resolve it using the following partially open implementation class Service<T0, T1>
.
In this scenario, T0
would be fixed, and T1
from Service
and IService
would be the same.
Ideally, it would look something like this:
container.Register<IService<>, Service<ExplicitType,>>(Lifestyle.Singleton);
but that's incorrect c#.
My question is:
Service<ExplicitType,>
if possible.I did not find the great documentation helpful on this matter.
This is certainly possible, but you can't express partially open generic types in C#. You will need to revert to Reflection. You can specify your partially open type as follows:
Type implementation =
typeof(Service<,>).MakeGenericType(
typeof(ExplicitType),
typeof(Service<,>).GetGenericTypeArguments()[1]));
Registration in Simple Injector is now child's play:
container.Register(typeof(IService<>), implementation);
does that make sense?
Your example is very 'generic' (no pun intended) so it is hard to draw conclusions from that, but there are absolutely good use cases for partially open generic types. That's why Simple Injector has OOTB support for them.
I did not find the great documentation helpful on this matter.
The documentation actually has information about this, but in the documentation it is called "partially-closed", which is probably rhe reason you didn't find it.