Unity and Generics

How can I register and resolve a shared object / interface in Unity? I am trying to stay away from the configuration file.

I'm looking for something like

IEnterpriseClient<IInterface1> for solutions EnterpriseClient<IInterface1>

Class signature

class EnterpriseClient<T> : IEnterpriseClient<T> where T : class

Thank!

+5
source share
2 answers

This is exactly what you think:

container.RegisterType<IEnterpriseClient<IInterface1>, EnterpriseClient<IInterface1>>( ... );

This is if you want only this closed, closed, generic account to be registered. For an open generic (and not just IInterface1) you can do:

container.RegisterType(typeof(IEnterpriseClient<>), typeof(EnterpriseClient<>), ... );

You mentioned that you tried this - what doesn't work?

+7
source

All Articles