问题描述
我可以添加一个名为实例structuremap这样的:
I can add a named instance to structuremap like this:
For<IFoo>().Add<Foo>().Named("FooOne");
我可以,然后用得到:
which I can then get with:
ObjectFactory.GetNamedInstance<IFoo>("FooOne");
和我可以通过注册这样通过在运行时的参数:
and I can pass parameters at runtime by registering like this:
For<IFoo>().Add<Foo>().Ctor<string>("someParam");
和获得一个实例是这样的:
and get an instance like this:
ObjectFactory.With("someParam").EqualTo("blah").GetInstance<IFoo>();
所有的罚款。但我想有一个命名实例,并传递一个参数。所以我注册这样的:
All fine. But I want to have a named instance and pass it a parameter. So I'm registering like this:
For<IFoo>().Add<Foo>().Named("FooOne").Ctor<string>("someParam");
但我不能获取命名实例并将其传递参数在运行时制定出语法??
我试图做这样的事情:
But I can't work out the syntax for getting a named instance AND passing it the parameter at runtime??I'm trying to do something like:
ObjectFactory.With("someParam").EqualTo("blah").GetNamedInstance<IFoo>("FooOne");
但structuremap不给我的选项GetNamedInstance添加参数后。我在哪里去了?
But structuremap doesn't give me the option to GetNamedInstance after adding the parameter. Where am I going wrong?
替代方法的建议也将是不错的。基本上我想要做的就是注册一个枚举的每个元素的具体类型,并使用枚举项目命名,并通过名称检索。但我需要能够传递一个参数在运行时的构造。
Alternative approach suggestions would also be good. Essentially what I'm trying to do is register a concrete type for each element of an enum, and use the enum item to name it and retrieve it by name. But I need to be able to pass a parameter to the constructor at runtime.
在此先感谢。
推荐答案
StructureMap不提供这样做的API。
StructureMap doesn't provide an API for doing that.
您可以使用一个抽象工厂。还有,可以在这里找到的堆栈溢出的例子很多的。
You can use an abstract factory. There are lot's of examples that can be found here on Stack Overflow.
他们几个是的并的。
这篇关于Structuremap - 我怎样才能得到一个命名实例,并在运行时传递的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!