Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 非泛型接口的统一寄存器泛型类型_C#_.net_Generics_Unity Container - Fatal编程技术网

C# 非泛型接口的统一寄存器泛型类型

C# 非泛型接口的统一寄存器泛型类型,c#,.net,generics,unity-container,C#,.net,Generics,Unity Container,(在我看来)我的设想非常直截了当,但我找不到解决办法 我有这种情况 public class Class<T> : IInterface where T : class { } public class类:接口,其中T:class { } 无法使接口成为通用接口(来自WCF库。) 所以我想像这样注册接口 container.RegisterType(typeof (IInterface ), typeof (Class<>)); container.Regis

(在我看来)我的设想非常直截了当,但我找不到解决办法

我有这种情况

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

}
public class类:接口,其中T:class
{ 
}
无法使接口成为通用接口(来自WCF库。)

所以我想像这样注册接口

container.RegisterType(typeof (IInterface ), typeof (Class<>));
container.RegisterType(typeof(IInterface),typeof(Class));
然后用T解决它

我怎么做?我错过了什么

我的意图是做类似的事情

container.Resolve<IInterface>(/* specify T */);
container.Resolve(/*指定T*/);
我错过了什么

你错过了一家工厂

想想看,没有神奇的小妖精在后台猜测你需要的类型。你需要提供它。通过在如下配置时明确说明
T
是什么:

container.RegisterType(
    typeof(IInterface),
    typeof(Class<SomeType>));
工厂可按以下方式注册:

container.RegisterInstance<IInterfaceFactory>(
    new InterfaceFactory(container));
public class InterfaceFactory : IInterfaceFactory
{
    private readonly IUnityContainer container;
    public InterfaceFactory(IUnityContainer container)
    {
        this.container = container;
    }

    public IInterface Create<T>()
    {
        return this.container.Resolve<Class<T>>();
    }
}
container.RegisterInstance<Func<Type, IInterface>>(
    type => container.Resolve(
        typeof(Class<>).MakeGenericType(type)));
这基本上是相同的,但现在内联到委托中。您的使用者现在可以依赖于
Func
而不是
IInterfaceFactory
,并将类型实例传递给委托

我个人更喜欢使用描述性界面,如
IInterfaceFactory
。这取决于您。

如果您不需要使用非受控接口解决问题,您可以创建自己的受控接口,该接口使用泛型并从非受控接口派生。然后可以注册打开的泛型并解析关闭的泛型类型

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

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

IUncontrolled instance = container.Resolve<IControlled<string>>();
公共接口i受控:i受控{}
受控制的公共类:IControlled{}
RegisterType(typeof(IControlled),typeof(Controlled));
IUncontrolled实例=container.Resolve();

您必须在类中指定一个类型,才能使typeof正常工作,可能您想将接口转换为特定类型的类,例如类?不,我没有-我想以通用方式实现此接口,并且我需要动态接口。@Tom:如果不使用Unity,您将如何实现此接口?请用一些代码更新您的问题,说明这一点。希望这能让我们更好地理解您想要实现的目标。如果我没有使用unity,我会使用您建议的工厂。但是我认为unity可以处理这个问题,因为我想在解决这个问题时告诉他什么是T。@Tom:你不能告诉unity这个,你也不应该告诉unity这个。这就是工厂模式的设计目的。您的解决方案对我来说太复杂了。我希望unity为我做这件事——我将用实际的T来解析,这样unity就可以将它注入到接口上注册的类中。非常优雅的解决方案!极好的解决方案!伟大-简单而优雅
public interface IControlled<T> : IUncontrolled {}
public class Controlled<T> : IControlled<T> {}

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

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