Castle Windsor: Register class with internal constructor?

后端 未结 3 495
慢半拍i
慢半拍i 2021-01-21 05:20

A Castle Windsor question: Is it possible to register a class that has an internal constructor with the container?

Thanks Joni

3条回答
  •  死守一世寂寞
    2021-01-21 05:56

    Yes, it is possible. Default component activator looks only for public constructors. You can either provide custom component activator for that component that would take internal constructor into account, or use for example factory to activate the component:

    var container = new WindsorContainer()
        .AddFacility()
        .Register( Component.For()
                       .UsingFactoryMethod( () => new Foo() ) );
    var foo = container.Resolve();
    

    However you should really reconsider making the .ctor internal in the first place. It is really rarely a good idea to do so, especially when you're exposing the class as a component to the outside world anyway.

提交回复
热议问题