将Castle Windsor xml配置转换为C#代码

时间:2010-01-29 21:52:33

标签: c# castle-windsor ioc-container

我想转换这样的内容:

<components>
    <component id=""service1"" service=""WindsorTests.IService, MyAssembly""         type=""WindsorTests.Service1, MyAssembly""/>
    <component id=""service2"" service=""WindsorTests.IService, MyAssembly"" type=""WindsorTests.Service2, MyAssembly""/>
    <component id=""consumer"" type=""WindsorTests.Consumer, MyAssembly"">
        <parameters>
            <services>
                <dictionary>
                    <entry key=""one"">${service1}</entry>
                    <entry key=""two"">${service2}</entry>
                </dictionary>
            </services>
        </parameters>
    </component>
</components>

进入这样的代码:

Container.AddComponentWithProperties<Consumer>(Container.ResolveAll<IService>());

任何人都有任何想法如何做到这一点。

注意:

我正在尝试执行本文中描述的内容,但不使用XML: Windsor Castle :- Inject Dictionary of Interfaces via configuration

1 个答案:

答案 0 :(得分:2)

container.Register(Component.For<Consumer>()
               .DynamicParameters((kernel, parameters) => 
                   parameters["services"] = new Dictionary<string, IService> {
                     {"one", kernel.Resolve<IService>("service1")},
                     {"two", kernel.Resolve<IService>("service2")},
                   }
               ));

请参阅fluent API wiki以供参考。