c# - 如何在 caSTLe-windsor 的 xml 配置中配置通用类型参数?

标签 c# castle-windsor xml-configuration

我有以下代码:

public class TestClass
{
    public string Foo { get; set; }
    public ITest<string> Test { get; set; }
}

[Convertible]
public interface ITest<T>
{
    T Param { get; set; }
}

[Convertible]
public class Test<T> : ITest<T>
{
    public T Param { get; set; }
    public string OtherParam { get; set; }
}

我想使用它

WindsorContainer container = new WindsorContainer(new XmlInterpreter());
var t = container.Resolve<TestClass>();

我不想使用 Fluent 配置,而是使用 xml 配置。另外,我想逃避 ITest 组件的显式注册。类似的是,它只能配置一个组件注册(TestClass),并且所有参数都可以在 节点中提供。但目前我无法创建工作配置,它创建了 null TestClass 对象或 TestClass 并将 Test 属性设置为 null

我的配置:

  <component id="Service.Main"
         type="ConsoleApplication1.TestClass"
         lifestyle="transient">
    <parameters>
      <foo>foo string</foo>
      <Test>
        <Param>param string</Param>
        <OtherParam>sdgsdfgdf</OtherParam>
      </Test>
    </parameters>
  </component>

也许有人可以建议正确的配置?谢谢

最佳答案

所以,我得到了 3.2.0 版本的源代码。 添加简单的控制台应用程序并通过调试检查不同版本。

这是可行的解决方案:

  <component id="Service.Main"
         type="ConsoleApplication1.TestClass"
         lifestyle="transient">
    <parameters>
      <foo>foo string</foo>
      <Test>
        <parameters type="ConsoleApplication1.Test`1[[System.String, mscorlib]], ConsoleApplication1">
          <Param>param string</Param>
          <OtherParam>sdgsdfgdf</OtherParam>
        </parameters>
      </Test>
    </parameters>
  </component>

重要说明:

  1. 我们应该用于后续的复杂属性,否则它无法“查看”所有参数列表。它只得到第一个 child :ConfigurationParametersInspector.cs, line 58
  2. 我们应该明确设置接口(interface)类型的属性所表示的参数类型。我们可以预期 type 属性应该用于 <Test> 节点,但实际上,只有子 XML 节点传递给选择属性类型的方法。

无论如何,使用 3.2.0 检查配置并且它可以工作。

关于c# - 如何在 caSTLe-windsor 的 xml 配置中配置通用类型参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21258649/

相关文章:

c# - 使用文本文件保存和加载 TextBox 或其他控件值

c# - ContextMenu 导致所有其他 UI 元素不可点击

c# - CaSTLe Windsor - 一个实现多个接口(interface)的类

dependency-injection - 无实现类型工厂问题

c# - 使用 Unity 作为子解析器的 CaSTLe windsor 解析管道

ssis - XML SSIS 配置文件

c# - 如何以编程方式将带有证书链的 pfx 导入证书存储区?

c# - 获取 CheckBoxList 的 ValueMembers

java - Spring Boot如何创建Auto配置类

java - 使用 MethodInvokingFactoryBean for XMLConfiguration 在 Spring bean 中加载文件