2

我有以下代码:

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),并且所有参数都可以在 <parameters> 节点中提供。但目前我未能创建工作配置,它创建了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>

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

4

1 回答 1

2

所以,我得到了 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,第 58 行
  2. 我们应该用接口类型显式设置属性表示的参数类型。我们可以期望type属性应该用于 <Test> 节点,但实际上,只有子 XML 节点传递给为属性选择类型的方法。

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

于 2014-01-21T14:16:43.637 回答