We use Unity dependency injection. We have a class that needs a type passed to it's constructor.
public interface ITest
{
}
public class Test : ITest
{
public Test(Type myType)
{
}
}
In the container boostrapper we register our type:
public static IUnityContainer Start()
{
IUnityContainer container = UnityInversionOfControlContainer.RegisterContainer();
container.RegisterType<ITest, Test>();
return container;
}
we resolve like so:
object actual = container.Resolve<ITest>(new ParameterOverride("myType", typeof(string)));
this give the following error:
Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "ITest", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The type String cannot be constructed. You must configure the container to supply this value.
At the time of the exception, the container was:
Resolving Test,(none) (mapped from Belastingdienst.Omvormers.Mapper.ITest, (none)) Resolving parameter "myType" of constructor Test(System.Type myType) Resolving System.String,(none) ---> System.InvalidOperationException: The type String cannot be constructed. You must configure the container to supply this value.
It appears that unity wants to resolve the type via container, and pass in an instance of the type, but I do need the Type.
Thoughts anyone?
container.Resolve<ITest>...
? It seems not to work with parameters of typeType
, I tried withstring
and it worked fine. Might be a bug in unity. – HaukingermyType
andtypeof(string)
as an instance not a type – Haitham Shaddad