How do I autoregister a generic class with a generic interface without registering EVERY type to it?

Feb 15, 2010 at 7:20 PM
Edited Feb 15, 2010 at 7:20 PM

This line for Unity:

unityContainer.RegisterType(typeof(IAction<>), typeof(Action<>));

effectively registers every class in the project to IAction/Action:

unityContainer.RegisterType<IAction<ObjectA>, Action<ObjectA>>();
unityContainer.RegisterType<IAction<ObjectB>, Action<ObjectB>>();
unityContainer.RegisterType<IAction<ObjectC>, Action<ObjectC>>();
unityContainer.RegisterType<IAction<UnrelatedObject>, Action<UnrelatedObject>>();

But, I only want specific objects to be registered. How would I do that? My guess is to add a custom attribute decorator to the specific classes.

public class ObjectB
{ [...] }

And try to use Unity.AutoRegistration. This is where I am stuck at:

        .As ?? // I'm guessing this is where I specify
        .With ?? // IAction<match> goes to Action<match>


[Stack Overflow Question]


Feb 5, 2014 at 10:08 AM
Hi, i've encountered similar problem. When i need to register some generic type i have to specify Generic interface and name of generic class that implements this interface, so all of Generic types must be specified directly to register them. Is there method that not requires to specify each generic class name and adding reference to containing assembly?

PS. Sorry for my English
Feb 5, 2014 at 11:25 PM
I think it is possible, though I don't fully what's your exact case. Please have a look into my stackoverflow answer and this blogpost to see if they answer your question. If not, please provide some code samples of what you're trying to achieve.