本文介绍了团结InjectionConstructor为multiparam构造覆盖只有单一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的构造函数的类:

 公共类酒吧
{
公共酒吧(的IFoo富,IFoo2 foo2的,IFoo3 foo3,IFooN欢,字符串文本)
{

}
}

我要在统一注册律师,并提供文本值:

  unity.RegisterType<酒吧,酒吧>(新InjectionConstructor(123)); 



不过,我不能这样做,因为是酒吧没有单一参数的构造函数。



有没有提供文本值而不指定的所有其他参数 ResolvedParameter℃的途径; IFooN> 等。我真不喜欢它,代码很多的,我每次改变酒吧的构造时间,我需要添加另一个ResolvedParameter


解决方案

团结不能做到这一点的开箱。你能做的最好的是:



container.RegisterType<酒吧及GT;(
新InjectionConstructor(
typeof运算(的IFoo)的typeof(IFoo2)的typeof(IFoo3)的typeof(IFooN),123));



或者您可以使用所提供的 SmartConstructor TecX 项目。 介绍一些背景。



注册是这样的:



container.RegisterType<酒吧及GT;(新SmartConstructor(文字,123));


I have a class with constructor like this:

public class Bar
{
    public Bar(IFoo foo, IFoo2 foo2, IFoo3 foo3, IFooN fooN, String text)
    {

    }
}

I want to register Bar in Unity and provide a value for text:

unity.RegisterType<Bar, Bar>(new InjectionConstructor("123"));

However I can't do this because there is no single parameter constructor for Bar.

Is there a way to provide a value for text without specifying all other parameters as ResolvedParameter<IFooN> etc.. I really don't like it, lot's of code, and every time I change a constructor of Bar I need to add another ResolvedParameter

解决方案

Unity can't do this out of the box. The best you can do is:

container.RegisterType<Bar>(
    new InjectionConstructor(
        typeof(IFoo), typeof(IFoo2), typeof(IFoo3), typeof(IFooN), "123"));

Or you can use the SmartConstructor provided by the TecX project. This blog post describes some background.

Registration would look like this:

container.RegisterType<Bar>(new SmartConstructor("text", "123"));

这篇关于团结InjectionConstructor为multiparam构造覆盖只有单一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 14:46