团结与通用

Wil*_*iam 5 .net c# generics inversion-of-control unity-container

如何在Unity中注册和解析通用对象/接口?我试图远离配置文件.

我正在寻找类似的东西

IEnterpriseClient<IInterface1> 解决 EnterpriseClient<IInterface1>

班级签名是

class EnterpriseClient<T> : IEnterpriseClient<T> where T : class
Run Code Online (Sandbox Code Playgroud)

谢谢!

Chr*_*res 7

这几乎就是你的想法:

container.RegisterType<IEnterpriseClient<IInterface1>, EnterpriseClient<IInterface1>>( ... );
Run Code Online (Sandbox Code Playgroud)

如果您只想要特定的封闭通用注册,那就是这样.对于开放式通用(不仅仅是IInterface1),您可以:

container.RegisterType(typeof(IEnterpriseClient<>), typeof(EnterpriseClient<>), ... );
Run Code Online (Sandbox Code Playgroud)

你提到你试过这个 - 什么不起作用?