将构造函数参数传递给结构映射中的命名实例,其中类型仅在运行时已知

发布于 2024-10-04 03:48:36 字数 524 浏览 0 评论 0原文

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

因此,虽然:

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

可以工作,但显然无法调用:

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

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

正如您可能想象的那样,我对这种方法并不满意,但我看不出有任何其他方法可以摆脱这种情况。

This is similar to the question asked at StructureMap - Override constructor arguments for a named instance, but different in the respect that I don't know the type at coding time and therefore cannot use the generic form of GetInstance().

So while:

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

will work, there is apparently no way to call:

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

I have a workaround using a private generic method and the MakeGenericMethod() on that private method's MethodInfo.

As you might imagine, I'm not really happy with that approach, but I cannot see any other way out of the situation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2024-10-11 03:48:36

您想要的方法是 GetNamedInstance(),当您使用 With() 方法时,该方法不可用。我确信添加它不会太难,也许您可​​以给 Jeremy Miller 发送电子邮件,看看他是否可以将其添加到 StructureMap 3 中。或者提交补丁:)

The method you want is GetNamedInstance(), which is not available when you use the With() method. I'm sure it would not be too hard to add it, maybe you could email Jeremy Miller and see if he can add it in StructureMap 3. Or submit a patch :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文