Getting an instance of a type by supplying the type name

Jun 29, 2015 at 3:35 PM
is there a way to have the following code:

setup:
registered all types into the container like this:
      unityContaner.register(Interface, ""type name as a string value");
Resolve:
     object obj = unityContaner.resolve<IInterface>("type name as a string value");
Jun 29, 2015 at 4:05 PM
You can do it pretty much as you've written. The one step missing is that you will need to provide a mapping between the interface and the implementation.

Using the same names as in the question it would be something like:
    public interface IInterface { }
    public class Interface : IInterface { }

    IUnityContainer container = new UnityContainer();
    // Map IInterface to the concrete "Interface" class and register using the class name
    container.RegisterType<IInterface, Interface>(typeof(Interface).Name);
    IInterface interfaceClass = container.Resolve<IInterface>(typeof(Interface).Name);