Passing parameters to UsingFactoryMethod in Castle Windsor

霸气de小男生 提交于 2019-12-10 09:56:46

问题


How do I pass dynamic parameters to a UsingFactoryMethod registration?

For example, I want to write something like:

container.Register(
   Component.For<IFoo>()
        .UsingFactoryMethod(return DoSomethingAndReturnInstance(paremeter)));

I need the parameters to be sent at runtime, like this:

container.Resolve<IFoo>(new { parameter = value });

How can it be done?


回答1:


CreationContext.AdditionalParameters has the values you pass to Resolve




回答2:


You just have to use

container.Register(
    Component.For<IFoo>()
        .UsingFactoryMethod((kernel, creationContext) =>
        {
            var parameter = creationContext.AdditionalArguments["parameter"];
            return DoSomethingAndReturnInstance(parameter);
        }));


来源:https://stackoverflow.com/questions/5453648/passing-parameters-to-usingfactorymethod-in-castle-windsor

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