Convert Castle Windsor xml config to C# code

点点圈 提交于 2019-12-13 19:52:21

问题


I want to convert something like this:

<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>

Into code like this:

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

Anyone have any ideas how to do this.

Note:

I am trying to do something like what is described in this post, but without using XML: Windsor Castle :- Inject Dictionary of Interfaces via configuration


回答1:


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

See the fluent API wiki for reference.



来源:https://stackoverflow.com/questions/2165427/convert-castle-windsor-xml-config-to-c-sharp-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!