开发者

Passing parameters to UsingFactoryMethod in Castle Windsor

How do I pass dynamic parameters to a U开发者_如何转开发singFactoryMethod 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?


CreationContext.AdditionalParameters has the values you pass to Resolve


You just have to use

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜