【问题标题】:Unity InjectionConstructor Passing Object arrayUnity InjectionConstructor 传递对象数组
【发布时间】:2013-02-27 15:31:50
【问题描述】:

我正在尝试使用统一注册依赖项并将值传递给构造函数,使用下面的代码

container.RegisterType<IProviderContext, MockOrderServiceProviderContext>(new InjectionConstructor(new Pharmacy {SiteId = 2,DistrictCode = "2"}));

但我得到了

"类型 OrderService.Business.MockOrderServiceProviderContext 确实 没有带参数的构造函数(Pharmacy)。”

构造函数。

public MockOrderServiceProviderContext(params object[] objects)
     {
         _object = objects;
     }

如何为对象数组传递构造函数值?

谢谢

【问题讨论】:

    标签: dependency-injection unity-container


    【解决方案1】:

    params 关键字是传递参数数组的语法糖。

    以下方法:

    public void MyMethod(params object[] args)
    {
    }
    

    可以通过以下两种方式调用,结果相同:

    MyMethod(new object(), new object());
    MyMethod(new []{ new object(), new object());
    

    当 Unity 查找构造函数时,它会看到一个带有 object[] 类型参数的构造函数。所以Unity提供的值一定是数组。

    InjectionConstructor 也使用了 params 关键字,其中每个参数都是一个值,可以转发给您自己的构造函数。如果您使用数组实例化InjectionConstructor,它将尝试使用数组的每个元素并将它们转发给您的类构造函数。

    为了阻止这种情况,我们需要两层包装,一层用于统一为您的类提供数组,另一层用于InjectionConstructor 使用第一个数组作为第一个也是唯一的参数。

    所以你应该使用以下内容:

    container.RegisterType<IProviderContext, MockOrderServiceProviderContext>(
        new InjectionConstructor(new []
        {
           new []
           {
               new Pharmacy { SiteId = 2, DistrictCode = "2" }
           }
        }));
    

    如果你想传递额外的项目,只需将它们添加到内部数组中:

    container.RegisterType<IProviderContext, MockOrderServiceProviderContext>(
        new InjectionConstructor(new []
        {
           new []
           {
               new Pharmacy { SiteId = 2, DistrictCode = "2" },
               new Hospital { SiteId = 5, DistrictCode="2" }
           }
        }));
    

    【讨论】:

    • 感谢有关参数和注入构造函数的解释
    • @JoR 没问题,我希望它有意义。
    • 非常有帮助的解释,即使是 3 年后!
    【解决方案2】:

    这就是我的工作方式。

    您必须将对象添加到数组中,然后将它们添加到容器数组中,以便它与构造函数参数签名匹配

    var objAr = new object[2];
    objAr[0] = new Pharmacy { SiteId = 3, DistrictCode = "2" };
    objAr[1] = new Facility { SiteID = 1, FacilityCode="Facility" };
    
    //Add the object array to another container array so that Unity Injection constructor can match the constructor.
    var containerArray = new object[1];
    containerArray[0] = objAr;
    
    container.RegisterType<IProviderContext, MockOrderServiceProviderContext>(new InjectionConstructor(containerArray));
    

    【讨论】:

      【解决方案3】:

      如果我错了,请纠正我,但你不是想用你的MockOrderServiceProviderContext 给一个不是数组的参数吗?

      您可以将new InjectionConstructor(..) 作为参数,或者如果您愿意,可以使用object,而不是object[]。

      【讨论】:

      • 其实没有。拥有对象数组的想法是我们可以传递多个对象。喜欢new InjectionConstructor(new Pharmacy {SiteId = 2,DistrictCode = "2"},new Hospital {ID =1,name="SSS"}谢谢
      • @joetjah 在 Unity 注册中,InjectionConstructor 的参数用于确定注入值时的参数。 InjectionConstructor 实际上并没有用作参数值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 2020-07-12
      • 2019-12-19
      相关资源
      最近更新 更多