StructureMap:将运行时参数传递给选定的构造函数

史蒂夫·基尼恩

我想选择一个构造函数并传递运行时参数。我知道如何选择具有注册表提供的参数的构造函数,也知道如何提供运行时参数。但是我看不到将两者结合在一起的方法。

班级:

public class SomeClass
{
    // This is the one I want to be the default by selecting it.
    public SomeClass(string arg1, AnArgClass arg2) { }

    // This is default if I don't purposely select it.
    public SomeClass(string arg1, string arg2, string arg3) { }
}

如何注册(我知道这不起作用):

ForConcreteType<SomeClass>()
    .Configure.SelectConstructor(() => new SomeClass(arg1?, arg2?));  I don’t see a way to get the runtime args in…

如果可以注册,这就是我将如何创建它并提供参数的方式:

var obj1 = _container.With(“arg1”).EqualTo(aRunTimeArg1)
    .With<AnArgClass>(aRunTimeArg2)
    .GetInstance<SomeClass>();

提前致谢。

(注意:我正在寻找一个StructureMap 3.x解决方案。一些看起来几乎可行的选项正在使用2.x语法,而3.x似乎不可用-否则它就会移动)

史蒂夫·基尼恩

我找到了一些解决方案。

  1. [DefaultConstructor]

将该属性标记为所需的构造函数,然后关闭。绝对不是我的第一选择,因为它将注册分为实现部分,以及使用IoC属性的其他问题。但这是最简单的,如果您在代码中没有IoC容器属性的问题,请尝试一下。

  1. Policies.ConstructorSelector()

下面是一种使用构造函数选择器策略查找构造函数的方法,并且有多个选择器按类型分隔。请注意,可以将其制成扩展方法解决方案,但我不想使其复杂化。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using StructureMap;
using StructureMap.Configuration.DSL;
using StructureMap.Pipeline;
using StructureMap.TypeRules;

public class SomeClass
{
    // This is the one I want to be the default by selecting it.
    public SomeClass(string arg1, AnArgClass arg2) { }

    // This is default if I don't purposely select it.
    public SomeClass(string arg1, string arg2, string arg3) { }
}

public class AnArgClass { }

public class SampleRegistry : Registry
{
    public SampleRegistry()
    {
        var selectors = new SelectorsList();
        Policies.ConstructorSelector(selectors);

        For<SomeClass>().Use<SomeClass>();
        selectors.Add<SomeClass>(new SelectorByTypes(new[] { typeof(string), typeof(AnArgClass) }));
    }
}

public class SelectorByTypes : IConstructorSelector
{
    private Type[] mArgumentsTypes;

    public SelectorByTypes(IEnumerable<Type> argumentsTypes)
    {
        mArgumentsTypes = argumentsTypes.ToArray();
    }

    public ConstructorInfo Find(Type pluggedType)
    {
        return pluggedType.GetConstructor(mArgumentsTypes); // GetConstructor() ext in SM.TypeRules
    }
}

public class SelectorsList : IConstructorSelector
{
    // Holds the selectors by type
    private Dictionary<Type, IConstructorSelector> mTypeSelectors = new Dictionary<Type, IConstructorSelector>();
    // The usual default, from SM.Pipeline
    private GreediestConstructorSelector mDefaultSelector = new GreediestConstructorSelector(); 

    public void Add<T>(IConstructorSelector selector)
    {
        mTypeSelectors.Add(typeof(T), selector);
    }

    public ConstructorInfo Find(Type pluggedType)
    {
        ConstructorInfo selected = null;
        if (mTypeSelectors.ContainsKey(pluggedType))
        {
            var selector = mTypeSelectors[pluggedType];
            selected = selector.Find(pluggedType);
        }
        else
        {
            selected = mDefaultSelector.Find(pluggedType);
        }
        return selected;
    }
}

作为扩展,它将类似于:

 For<SomeClass>()
      .Use<SomeClass>()
      .SetConstructor(selectors, new Type[] { typeof(string), typeof(AnArgClass) });


public static class Extensions
{
    public static SmartInstance<TConcreteType, TPluginType> SetConstructor<TConcreteType, TPluginType>(
        this SmartInstance<TConcreteType, TPluginType> instance,
        ConstructorSelectors constructors,
        IEnumerable<Type> types)
        where TConcreteType : TPluginType
    {
        constructors.Add(typeof(TPluginType), new ArgTypesConstructorSelector(types));
        return instance;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用structuremap在运行时将对象传递给类构造函数

来自分类Dev

如何通过shell脚本将运行时参数传递给c中的函数

来自分类Dev

如何在运行时使用简单的注入器将值传递给实例集合的构造函数?

来自分类Dev

为什么C ++允许在运行时将数组大小传递给函数以构造固定大小的数组?

来自分类Dev

更改构造函数参数运行时

来自分类Dev

如何调试“传递给C运行时函数的无效参数”?

来自分类Dev

将参数传递给类构造函数

来自分类Dev

将运行时参数传递给Node.js子进程

来自分类Dev

将运行时参数传递给odeint积分器

来自分类Dev

如何将运行时参数传递给EF Core谓词表达式

来自分类Dev

在运行时将参数传递给std :: bind到调度队列

来自分类Dev

通过cygwin从Shell脚本运行时,如何将参数传递给Powershell脚本?

来自分类Dev

使用bash脚本运行时如何将参数传递给python脚本?

来自分类Dev

在运行时将txt文件作为参数传递给脚本

来自分类Dev

Python:在运行时将任意值传递给 chrome 扩展中的 js 函数

来自分类Dev

将IoC / DI容器与运行时相关的构造函数参数一起使用

来自分类Dev

带运行时构造函数参数的委托工厂代理?

来自分类Dev

构造函数的Symfony依赖注入运行时参数

来自分类Dev

带有运行时构造函数参数的Spring bean

来自分类Dev

Scala在运行时获取构造函数参数

来自分类Dev

在运行时将变量传递给方法

来自分类Dev

将Burrsuite的JAR传递给Java运行时

来自分类Dev

从 npm 运行时将选项传递给 mocha

来自分类Dev

Structuremap-如何在运行时获取命名实例并传递参数?

来自分类Dev

在 VBA 中将列表框作为函数参数传递给我一个运行时错误 13

来自分类Dev

如何将字段构造函数参数传递给函数?

来自分类Dev

Java:在运行时将类作为参数传递

来自分类Dev

C#在运行时绑定事件时将附加参数传递给事件处理程序

来自分类Dev

Scala-根据构造函数参数的运行时类型选择构造函数

Related 相关文章

  1. 1

    使用structuremap在运行时将对象传递给类构造函数

  2. 2

    如何通过shell脚本将运行时参数传递给c中的函数

  3. 3

    如何在运行时使用简单的注入器将值传递给实例集合的构造函数?

  4. 4

    为什么C ++允许在运行时将数组大小传递给函数以构造固定大小的数组?

  5. 5

    更改构造函数参数运行时

  6. 6

    如何调试“传递给C运行时函数的无效参数”?

  7. 7

    将参数传递给类构造函数

  8. 8

    将运行时参数传递给Node.js子进程

  9. 9

    将运行时参数传递给odeint积分器

  10. 10

    如何将运行时参数传递给EF Core谓词表达式

  11. 11

    在运行时将参数传递给std :: bind到调度队列

  12. 12

    通过cygwin从Shell脚本运行时,如何将参数传递给Powershell脚本?

  13. 13

    使用bash脚本运行时如何将参数传递给python脚本?

  14. 14

    在运行时将txt文件作为参数传递给脚本

  15. 15

    Python:在运行时将任意值传递给 chrome 扩展中的 js 函数

  16. 16

    将IoC / DI容器与运行时相关的构造函数参数一起使用

  17. 17

    带运行时构造函数参数的委托工厂代理?

  18. 18

    构造函数的Symfony依赖注入运行时参数

  19. 19

    带有运行时构造函数参数的Spring bean

  20. 20

    Scala在运行时获取构造函数参数

  21. 21

    在运行时将变量传递给方法

  22. 22

    将Burrsuite的JAR传递给Java运行时

  23. 23

    从 npm 运行时将选项传递给 mocha

  24. 24

    Structuremap-如何在运行时获取命名实例并传递参数?

  25. 25

    在 VBA 中将列表框作为函数参数传递给我一个运行时错误 13

  26. 26

    如何将字段构造函数参数传递给函数?

  27. 27

    Java:在运行时将类作为参数传递

  28. 28

    C#在运行时绑定事件时将附加参数传递给事件处理程序

  29. 29

    Scala-根据构造函数参数的运行时类型选择构造函数

热门标签

归档