简体   繁体   中英

Unity and Generics

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?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM