简体   繁体   English

团结与通用

[英]Unity and Generics

how do i register and resolve a generic object/interface in Unity? 如何在Unity中注册和解析通用对象/接口? I'm trying to stay away from the config file. 我试图远离配置文件。

I'm looking for something like 我正在寻找类似的东西

IEnterpriseClient<IInterface1> to resolve to EnterpriseClient<IInterface1> IEnterpriseClient<IInterface1>解析为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: 对于开放式通用(不仅仅是IInterface1),您可以:

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

You mentioned you'd tried this - what's not working? 你提到你试过这个 - 什么不起作用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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