Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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# Unity:如何指定在解析其他类型时使用类型的特定实例_C#_.net_Unity Container - Fatal编程技术网

C# Unity:如何指定在解析其他类型时使用类型的特定实例

C# Unity:如何指定在解析其他类型时使用类型的特定实例,c#,.net,unity-container,C#,.net,Unity Container,我在Unity中尝试以下内容: 我有一个具有以下构造函数的类型 public Type1(Type2 firstDependency, Type3 secondDependency) 使用Unity解析Type1时,我想为Type2指定要注入的特定实例。Type2的此特定实例未在容器中注册类型3已在容器中注册,应像往常一样进行解析 更具体地说,考虑类型1>代码>是文档浏览者< /代码>类。代码>类型2是一个特定的文档类型3是一个拼写检查器 我希望能够解析只有在运行时才知道的文档的Documen

我在Unity中尝试以下内容:

我有一个具有以下构造函数的类型

public Type1(Type2 firstDependency, Type3 secondDependency)
使用Unity解析
Type1
时,我想为
Type2
指定要注入的特定实例。
Type2
的此特定实例未在容器中注册<代码>类型3已在容器中注册,应像往常一样进行解析

更具体地说,考虑<代码>类型1>代码>是<代码>文档浏览者< /代码>类。代码>类型2是一个特定的

文档
<代码>类型3是一个
拼写检查器

我希望能够解析只有在运行时才知道的
文档的
DocumentViewer
。可以为不同的
文档创建多个
DocumentViewer
实例


我怎样才能做到这一点呢?

这里是我举的一个快速示例,您可以使用RegisterInstance,也可以使用Lifetime management Claas

static void Main(string[] args)
{
    IUnityContainer container = new UnityContainer();

    container.RegisterType<Type1>();

    container.RegisterInstance<Type2>(new Type2());

    Type1 t = container.Resolve<Type1>();

    Type2 t2 = container.Resolve<Type2>();

    Type3 t3 = container.Resolve<Type3>();

    Console.ReadKey();
}

public class Type1
{
}

public class Type2
{
}

public class Type3
{
    private Type1 t;
    private Type2 t2;
    public Type3(Type1 t, Type2 t2)
    {
        this.t = t;
        this.t2 = t2;
    }
}
static void Main(字符串[]args)
{
IUnityContainer容器=新的UnityContainer();
container.RegisterType();
container.RegisterInstance(新的Type2());
type1t=container.Resolve();
Type2 t2=container.Resolve();
Type3 t3=container.Resolve();
Console.ReadKey();
}
公共类类型1
{
}
公共类类型2
{
}
公共类类型3
{
私人1型t;
私人2型t2;
公共类型3(类型1 t、类型2 t2)
{
t=t;
这1.t2=t2;
}
}

更新:我在构造函数中包含了一个带有两个参数的类型,以显示它也可以被解析。

尝试使用命名实例:


IUnityContainer container = new UnityContainer();
container.RegisterType<Type1>();
container.RegisterType<Type2>("Instance 1", new ContainerControlledLifetimeManager());
container.RegisterType<Type2>("Instance 2", new ContainerControlledLifetimeManager());
container.RegisterType<Type3>();

Type1 type1 = container.Resolve<Type1>();
if (type1 == ...)
{
  Type2 instance1 = container.Resolve<Type2>("Instance 1");
}
else
{
  Type2 instance2 = ontainer.Resolve<Type2>("Instance 2");
}


IUnityContainer容器=新的UnityContainer();
container.RegisterType();
RegisterType(“实例1”,新的ContainerControlledLifetimeManager());
RegisterType(“实例2”,新的ContainerControlledLifetimeManager());
container.RegisterType();
Type1 Type1=container.Resolve();
如果(类型1==…)
{
Type2 instance1=container.Resolve(“实例1”);
}
其他的
{
Type2 instance2=container.Resolve(“实例2”);
}
您可以对类型1进行一些检查,并决定需要哪个类型2的实例。请注意,“new ContainerControlled LifetimeManager()”参数将初始化抵制类型的单例实例,因此您将始终获得类型2的相同实例

更新:与接口相同。希望这有帮助


IUnityContainer container = new UnityContainer();
container.RegisterType<TextDocument>();
container.RegisterType<ImageDocument>();
container.RegisterType(typeof (IView), typeof (TextView), "Text", new ContainerControlledLifetimeManager());
container.RegisterType(typeof (IView), typeof (ImageView), "Image", new ContainerControlledLifetimeManager());

IDocument document = container.Resolve<TextDocument>();

IView view = null;
if (document is TextDocument)
{
    view = container.Resolve<IView>("Text");
}
else
{
    view = container.Resolve<IView>("Image");
}

view.Show();

IUnityContainer容器=新的UnityContainer();
container.RegisterType();
container.RegisterType();
RegisterType(typeof(IView),typeof(TextView),“Text”,新的ContainerControlledLifetimeManager());
RegisterType(typeof(IView),typeof(ImageView),“Image”,新的ContainerControlledLifetimeManager();
IDocument document=container.Resolve();
IView view=null;
如果(文档为文本文档)
{
视图=容器。解析(“文本”);
}
其他的
{
视图=容器。解析(“图像”);
}
view.Show();

如果您有一个具有多个构造函数的类,那么可以使用“InjectionConstructor”属性来决定Unity容器使用哪个构造函数。这使您可以手动设置一些参数


public class Test
{
    public Test()
    {
    }

    // Always use the following constructor
    [InjectionConstructor]
    public Test(Type1 type1) : this()
    {
    }

    public Test(Type1 type1, Type2 type2) : this(type1)
    {
    }
}
使用工厂

public class Type1Factory
{
  private Type3 type3;

  public Type1Factory(Type3 _type3)
  {
     type3 = _type3;
  }

  public GetType1(Type2 type2)
  {
    return new Type1(type2, type3);
  }
}
可以这样说:

// SpellingChecker is subclass of Type3
IUnityContainer container = new UnityContainer();
container.RegisterType<Type3>(typeof(SpellingChecker));

// DocumentViewer is subclass of Type2
Type1Factory factory = container.Resolve<Type1Factory>();
Type1 type1 = factory.GetType1(new DocumentViewer());
//SpellingChecker是Type3的子类
IUnityContainer容器=新的UnityContainer();
container.RegisterType(typeof(拼写检查器));
//DocumentViewer是Type2的子类
Type1Factory工厂=container.Resolve();
Type1 Type1=factory.GetType1(新的DocumentViewer());

这假设您只是试图使用Unity来解决Type3依赖关系,并且您无法控制Type1中的构造函数。如果您可以编辑Type1,请使用Alexader R.的建议,使Unity仅解析单参数构造函数。

您可以使用容器层次结构,请阅读我对类似问题的回答:


唯一的区别是您应该在子容器中使用
RegisterInstance()
,而不是
RegisterType()
。也许不是,这取决于您是否在代码之外创建了实例。

很抱歉,我仍然不明白为什么无法使用下面答案中的代码解析文档视图的实例。什么是“文档只在运行时已知”?如果您不知道文档实例的确切类型,请考虑使用接口。用另一种方式说:我想解析一种类型,但只让容器自己解决一些依赖项。我想自己提供其他实例。RegisterInstance也可以实现单例效果