【问题标题】:How to configure generic type parameters in xml configuration for castle-windsor?如何在 Castle-windsor 的 xml 配置中配置泛型类型参数?
【发布时间】:2014-02-11 01:51:58
【问题描述】:

我有以下代码:

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>

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

【问题讨论】:

    标签: c# castle-windsor xml-configuration


    【解决方案1】:

    所以,我得到了 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. 我们应该用于后续的复杂属性,否则它无法“看到”所有参数列表。它只有第一个孩子:ConfigurationParametersInspector.cs, line 58
    2. 我们应该明确设置接口类型的属性所代表的参数类型。我们可以预期 type 属性应该用于 &ltTest> 节点,但实际上,只有子 XML 节点传递给为属性选择类型的方法。

    使用 3.2.0 检查任何配置,并且可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      相关资源
      最近更新 更多