Reputation: 7457
I have a generic interface, say IGeneric
which I want to resolve dynamically based on the types that I have as string
. To make it clearer, I can resolve the dependency like this:
container.Resolve<IGeneric<AType>>();
But my question is how to resolve it when I have this:
var type = "AType";
Is there any way apart from (better than) registering every possible class that can be used in place of AType
as a named instance?
Upvotes: 2
Views: 1637
Reputation: 52230
Use the prototype of Resolve that accepts a Type
.
Type type = System.Type.GetType("AType");
var instance = container.Resolve(type);
Or (not sure which one you want) to resolve for IGeneric<Atype>
use:
Type genericType = typeof(IGeneric<>);
Type specificType = genericType.MakeGenericType(new [] {Type.GetType("AType")});
var instance = container.Resolve(specificType );
Upvotes: 2