统一登记泛型类型的非通用接口 [英] Unity register generic type for non generic interface

查看:154
本文介绍了统一登记泛型类型的非通用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况看(我)非常简单,但我无法找到一个解决方案。

我有这样的情况下

 公共类类< T> :IInterface其中T:类
{

}
 

界面不能进行普通的(来自WCF LIB来了。)

所以我要注册这样的接口

  container.RegisterType(typeof运算(IInterface)的typeof(类<>));
 

,然后以T解决这个问题。

我该怎么办呢?我在想什么?

我的目的是做这样的事情

  container.Resolve< IInterface>(/ *指定T * /);
 

解决方案

如果你不这样做的需要的解析使用不受控制的界面,你可以使自己的控制界面,使用泛型和派生不受控制的接口。然后,你可以注册开放式泛型和解决封闭式泛型类型。

 公共接口IControlled< T> :IUncontrolled {}
公共类受控< T> :IControlled< T> {}

container.RegisterType(typeof运算(IControlled<>)的typeof(控制<>));

IUncontrolled例如= container.Resolve< IControlled<字符串>>();
 

my scenario looks (to me) very straight forward, but I couldn't find a resolution.

I have this scenario

public class Class<T> : IInterface where T : class
{ 

}

the interface cannot be made generic (coming from WCF lib.)

so I want to register the interface like this

container.RegisterType(typeof (IInterface ), typeof (Class<>));

and then resolve it with T

How can I do it? What am I missing?

my intention is doing something like

container.Resolve<IInterface>(/* specify T */);

解决方案

If you don't need to resolve using the uncontrolled interface, you can make your own controlled interface which uses generics and derives from the uncontrolled interface. Then you can register the open generic and resolve the closed generic types.

public interface IControlled<T> : IUncontrolled {}
public class Controlled<T> : IControlled<T> {}

container.RegisterType(typeof(IControlled<>), typeof(Controlled<>));

IUncontrolled instance = container.Resolve<IControlled<string>>();

这篇关于统一登记泛型类型的非通用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆