将构造函数参数传递给结构映射中的命名实例,其中类型仅在运行时已知
这类似于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要的方法是 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 :)