how do i register and resolve a generic object/interface in Unity? I'm trying to stay away from the config file.
I'm looking for something like
IEnterpriseClient<IInterface1>
to resolve to EnterpriseClient<IInterface1>
The class signature is
class EnterpriseClient<T> : IEnterpriseClient<T> where T : class
Thanks!
It's pretty much exactly what you'd think:
container.RegisterType<IEnterpriseClient<IInterface1>, EnterpriseClient<IInterface1>>( ... );
That's if you only want that particular closed generic registered. For the open generic (not just IInterface1), you can do:
container.RegisterType(typeof(IEnterpriseClient<>), typeof(EnterpriseClient<>), ... );
You mentioned you'd tried this - what's not working?
Look at this question for XML configuration: Unity 2.0 registering generic types via XML
and http://davidhayden.com/blog/dave/archive/2008/03/25/UnityDependencyInjectionOpenGenericTypes.aspx for code configuration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.