Interface NET-如何在运行时选择接口的实现?

Interface NET-如何在运行时选择接口的实现?,interface,runtime,spring.net,Interface,Runtime,Spring.net,在Spring.NET IoC的所有示例中,我看到了如下内容: interface IClass; class ClassA : IClass; class ClassB : IClass, IClass c = [get me all implementations of IClass, and choose the one with GetType().FullName == myVariableContainingFullTypeNameOfObje

在Spring.NET IoC的所有示例中,我看到了如下内容:

 interface IClass;

 class ClassA : IClass;

 class ClassB : IClass, 
IClass c = [get me all implementations of IClass, and choose the one with 
            GetType().FullName == myVariableContainingFullTypeNameOfObjectIWant]
然后在config.xml文件中,类似于:

[object id="IClass" type="ClassB, Spring.Net.Test" /]
但我确实需要在配置文件中执行类似的操作,如果接口:

[object id="IClass" type="ClassA, Blah" /]

[object id="IClass" type="ClassB, Blah" /]
然后在
\u运行时
中,我从它们中进行选择。大概是这样的:

 interface IClass;

 class ClassA : IClass;

 class ClassB : IClass, 
IClass c = [get me all implementations of IClass, and choose the one with 
            GetType().FullName == myVariableContainingFullTypeNameOfObjectIWant]
我怎么能做这样的事


非常感谢

也许你可以试试这个:

[object id=“Blah.ClassA”type=“ClassA,Blah”/

[objectid=“Blah.ClassB”type=“ClassB,Blah”/


IClass=(IClass)ApplicationContext.GetObject(myVariableContainingFullTypeNameOfObjectIWant)

也许你可以试试这个:

[object id=“Blah.ClassA”type=“ClassA,Blah”/

[objectid=“Blah.ClassB”type=“ClassB,Blah”/


IClass=(IClass)ApplicationContext.GetObject(myVariableContainingFullTypeNameOfObjectIWant)

我以前也做过类似的事情,采取了与法比亚诺建议的方法非常相似的方法

示例配置:

下面是一些使用WebApplicationContext的通用示例代码:

        IApplicationContext context = new XmlApplicationContext(locations);
        IClass c = (IClass)context.GetObject(declarationId);
有几件事需要注意:

  • 传入您希望使用的声明的id而不是类型名称,因此变量declarationId的值为“ClassAInstance”或“ClassBInstance”
  • XmlApplicationContext(和WebApplicationContext)的构造函数接受字符串值数组的参数;变量locations将是一个配置资源数组,用于搜索id为declarationId的对象。这里不能使用泛型列表,它必须是实际的字符串数组
  • 上面第2点的一个有趣含义是,您实际上可以控制ApplicationContext知道哪些配置资源:当您调用GetObject()方法时,ApplicationContext将只在数组位置[]中给定的配置资源中搜索您的对象。这意味着,您可以使用多个配置资源,而不是在一个文件中列出所有可能的配置,每个配置都具有唯一的id,每个配置资源包含一个对象声明,每个配置资源具有相同的id:

    Config1.xml:

    Config2.xml:

    但在实例化对象时,您可以控制创建的对象不是基于声明ID,在这两种情况下都是“IClassInstance”,而是通过更改位置[]数组来包含要使用的配置资源,在本例中为Config1.xml或Config2.xml

    希望这是有用的


    安德鲁

    我以前也做过类似的事情,采取了与法比亚诺建议的方法非常相似的方法

    示例配置:

    下面是一些使用WebApplicationContext的通用示例代码:

            IApplicationContext context = new XmlApplicationContext(locations);
            IClass c = (IClass)context.GetObject(declarationId);
    
    有几件事需要注意:

  • 传入您希望使用的声明的id而不是类型名称,因此变量declarationId的值为“ClassAInstance”或“ClassBInstance”
  • XmlApplicationContext(和WebApplicationContext)的构造函数接受字符串值数组的参数;变量locations将是一个配置资源数组,用于搜索id为declarationId的对象。这里不能使用泛型列表,它必须是实际的字符串数组
  • 上面第2点的一个有趣含义是,您实际上可以控制ApplicationContext知道哪些配置资源:当您调用GetObject()方法时,ApplicationContext将只在数组位置[]中给定的配置资源中搜索您的对象。这意味着,您可以使用多个配置资源,而不是在一个文件中列出所有可能的配置,每个配置都具有唯一的id,每个配置资源包含一个对象声明,每个配置资源具有相同的id:

    Config1.xml:

    Config2.xml:

    但在实例化对象时,您可以控制创建的对象不是基于声明ID,在这两种情况下都是“IClassInstance”,而是通过更改位置[]数组来包含要使用的配置资源,在本例中为Config1.xml或Config2.xml

    希望这是有用的

    安德鲁