What am I missing?
You are missing a factory.
Think about it, there are no magical leprechauns working on the background, guessing the type you need. You need to provide it. Or explicitly indicating that T when setting:
container.RegisterType( typeof(IInterface), typeof(Class<SomeType>));
Or by creating a factory where you pass T at runtime:
public interface IInterfaceFactory { IInterface Create<T>(); }
factory can be registered as follows:
container.RegisterInstance<IInterfaceFactory>( new InterfaceFactory(container));
And the implementation may look like this:
public class InterfaceFactory : IInterfaceFactory { private readonly IUnityContainer container; public InterfaceFactory(IUnityContainer container) { this.container = container; } public IInterface Create<T>() { return this.container.Resolve<Class<T>>(); } }
Now you can enter IInterfaceFactory into consumers who need to work with IInterface , and they can request the version they need by calling the Create<T>() method.
UPDATE
If you think this is too much code, you can also register the factory delegate as follows:
container.RegisterInstance<Func<Type, IInterface>>( type => container.Resolve( typeof(Class<>).MakeGenericType(type)));
This is basically the same, but is now included in the delegate. Now your consumers can depend on Func<Type, IInterface> instead of IInterfaceFactory and pass the type instance to the delegate.
I personally prefer to use a descriptive interface like IInterfaceFactory . It depends on you.