Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Castle Windsor: Register class with internal constructor?

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

Thanks Joni

like image 722
joniba Avatar asked May 14 '26 01:05

joniba


1 Answers

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<FactorySupportFacility>()
    .Register( Component.For<Foo>()
                   .UsingFactoryMethod( () => new Foo() ) );
var foo = container.Resolve<Foo>();

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.

like image 176
Krzysztof Kozmic Avatar answered May 19 '26 00:05

Krzysztof Kozmic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!