我有以下代码:
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),并且所有参数都可以在
我的配置:
<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>
重要说明:
- 我们应该用于后续的复杂属性,否则它无法“查看”所有参数列表。它只得到第一个 child :ConfigurationParametersInspector.cs, line 58
- 我们应该明确设置接口(interface)类型的属性所表示的参数类型。我们可以预期 type 属性应该用于 <Test> 节点,但实际上,只有子 XML 节点传递给选择属性类型的方法。
- 如果我们不提供 type 属性,则会失败并出现 ConverterException: DefaultComplexConverter.cs, line 83
- 在这里,我们得到带有type属性的
(以及松散的父节点属性,因此如果我们为 设置type,它将丢失:ConfigurationParametersInspector.cs, line 59 - 这是根据类型属性选择接口(interface)实现的方法:DefaultComplexConverter.cs, line 75
无论如何,使用 3.2.0 检查配置并且它可以工作。
关于c# - 如何在 caSTLe-windsor 的 xml 配置中配置通用类型参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21258649/