简体   繁体   English

如何使用具有 2 种不同情况的 StructureMap 获取通用 object 的实例?

[英]How to get instance of generic object using StructureMap with 2 different cases?

I have following interface我有以下界面

 public interface IBuilder<T>
 {
    T Create(string param);
 }

with many classes that implement the interface above.有许多实现上述接口的类。 One of them is:其中之一是:

 public class ConcreteABuilder : IBuilder<ConcreteA>
 {
        public ConcreteA Create(string param)
        {
            return new ConcreteA();
        }
 }

I'm using StructureMap to register all classes that implement IBuilder<>我正在使用 StructureMap 注册所有实现IBuilder<>的类

Scan(x =>
{
      x.TheCallingAssembly();
      x.AddAllTypesOf(typeof(IBuilder<>));
});    

Now, I have 2 cases现在,我有2个案例

EDITED已编辑

I get the types(in both cases) in the form of System.Type我以System.Type的形式获得类型(在两种情况下)

Case 1情况1

At runtime I get any T type( System.Type ) (eg typeof(ConcreteA) ) and I need to get the matched builder instance.在运行时我得到任何T类型( System.Type )(例如typeof(ConcreteA) ),我需要得到匹配的构建器实例。 In this case it must return ConcreteABuilder instance.在这种情况下,它必须返回ConcreteABuilder实例。

Case 2案例2

At runtime I get the type( System.Type ) of some implemented IBuilder(eg typeof(ConcreteABuilder) ) and I need to get the matched builder instance.在运行时,我得到了一些实现的 IBuilder 的类型( System.Type )(例如typeof(ConcreteABuilder) ),我需要得到匹配的构建器实例。 In this case it must return ConcreteABuilder instance.在这种情况下,它必须返回ConcreteABuilder实例。

How using StructureMap's ObjectFactory to solve Case1 & Case2?如何使用 StructureMap 的 ObjectFactory 解决 Case1 & Case2?

Thank you谢谢

Use this in StructureMap configuration在 StructureMap 配置中使用它

x.ConnectImplementationsToTypesClosing(typeof(IBuilder<>))

now to resolve generic type at runtime现在在运行时解析泛型类型

Type openType = typeof(IBuilder<>);//generic open type
var type = openType.MakeGenericType(modelType);//modelType is your runtime type

var builder = StructureMap.ObjectFactory.Container.GetInstance(type);//should get your ConcreteABuilder 

I think what you're looking for is registering your types with:我认为您正在寻找的是注册您的类型:

x.ConnectImplementationsToTypesClosing(typeof(IBuilder<>))

Then asking the container for an IBuilder<ConcreteA> or a ConcreteABuilder will return a ConcreteABuilder ... now the problem is that since you don't know the type until runtime (selected by the user or something?), you can only use the non-generic version:然后向容器询问IBuilder<ConcreteA>ConcreteABuilder将返回一个ConcreteABuilder ...现在的问题是,由于您直到运行时才知道类型(由用户选择或其他什么?),您只能使用非通用版本:

object someBuilder = ObjectFactory.GetInstance(thePassedInTypeAtRuntime);
... then use reflection to invoke the createMethod

or或者

dynamic someBuilder = (dynamic)ObjectFactory.GetInstance(thePassedInTypeAtRuntime);
....

and somewhere where you actually know that you're asking for a an IBuilder that can return ConcreteA在某个地方,您实际上知道您正在要求一个可以返回 ConcreteA 的 IBuilder

ConcreteA myA = someBuilder.Create(someParams);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM