Structuremap - 我怎样才能得到一个命名实例,并在运行时传递的参数? [英] Structuremap - how can I get a named instance and pass parameter at runtime?

查看:151
本文介绍了Structuremap - 我怎样才能得到一个命名实例,并在运行时传递的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以添加一个名为实例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 - 我怎样才能得到一个命名实例,并在运行时传递的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆