统一登记泛型类型的非通用接口接口、类型

2023-09-03 21:48:02 作者:心ф冇ba砍伱の刀

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

我有这样的情况下

 公共类类< 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>>();