【问题标题】:Passing constructor arguments to a Named Instance In Structure Map Where Type is Only known at Run time将构造函数参数传递给结构映射中的命名实例,其中类型仅在运行时已知
【发布时间】:2011-05-13 23:00:12
【问题描述】:

这类似于StructureMap - Override constructor arguments for a named instance 提出的问题,但不同之处在于我在编码时不知道类型,因此无法使用 GetInstance() 的通用形式。

所以当:

ObjectFactory.With(IFoo).GetInstance<IBar>("foobar");

会起作用,显然没有办法调用:

ObjectFactory.With(IFoo).GetInstance(typeof(IBar), "foobar");

我有一个使用私有泛型方法和该私有方法的 MethodInfo 上的 MakeGenericMethod() 的解决方法。

如您所想,我对这种方法并不满意,但我看不出有任何其他出路。

【问题讨论】:

    标签: structuremap ioc-container


    【解决方案1】:

    你想要的方法是GetNamedInstance(),当你使用With()方法时是不可用的。我相信添加它不会太难,也许你可以给 Jeremy Miller 发电子邮件,看看他是否可以在 StructureMap 3 中添加它。或者提交一个补丁 :)

    【讨论】:

    • 感谢您的回复。我实际上是在为 IContainer 编程(我认为这是首选方法)。 GetNamedInstance() 不是接口的一部分。看起来 GetInstance() 的重载似乎正在取代 GetNamedInstance()。我可能会做一个补丁,但我真的想先确保我没有错过任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多