我的方案看起来(对我来说)非常简单,但我找不到解决方案。
我有这个场景
public class Class<T> : IInterface where T : class
{
}
接口(interface)不能通用(来自 WCF 库。)
所以我想这样注册接口(interface)
container.RegisterType(typeof (IInterface ), typeof (Class<>));
然后用T解决
我该怎么做?我错过了什么?
我的意图是做类似的事情
container.Resolve<IInterface>(/* specify T */);
最佳答案
如果您不需要使用非受控接口(interface)进行解析,您可以创建自己的受控接口(interface),它使用泛型并派生自非受控接口(interface)。然后您可以注册开放泛型并解析封闭泛型类型。
public interface IControlled<T> : IUncontrolled {}
public class Controlled<T> : IControlled<T> {}
container.RegisterType(typeof(IControlled<>), typeof(Controlled<>));
IUncontrolled instance = container.Resolve<IControlled<string>>();
关于c# - Unity 为非泛型接口(interface)注册泛型类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18482247/