I want to create types using Unity. I do not know the specific type on compile time. I create a Type instance from string using the following code:
var theType = Type.GetType( "System.Exception" );
When I try to resolve the type using the container I get an Exception as he tries to use the constructor with the most parameters:
_Container.Resolve( theType ); // <- uses a constructor with message, etc.
I know I could either configure the resolving using a config file containing something like
<register type="Exception">
<constructor />
</register>
or by code using
container.RegisterType<Exception>(new InjectionConstructor());
The latter possibility does not work on System.Exception for obvious reasons...
Is there a possibility to tell the Resolve method to use the ()-constructor? ResolverOverride seems to help to pass parameters, but I want to use the default constructor and fail otherwise. Right know I call Activator.CreateInstance, but I would like to use Unity consistently.
Note: Exception was just used as a sample class that contains multiple constructors. As a plugin created by another developer might have too, as I have no control over the available constructors using an interface.
Edit: My idea is, that if there is a configuration in the config file Unity would use the constructor configured there. If not use the default constructor. I do not like the idea of a fallback. Try first with Unity, then use Activator
Activator.CreateInstanceseems like the correct tool for the job. - Dan BryantException- Jamiec