温莎城堡:向内部构造函数注册类吗? [英] Castle Windsor: Register class with internal constructor?

查看:63
本文介绍了温莎城堡:向内部构造函数注册类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

温莎城堡的问题:
是否可以在容器中注册一个具有内部构造函数的类?

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

感谢
Joni

Thanks Joni

推荐答案

是的,有可能。默认组件激活器仅查找公共构造函数。
您可以为要考虑内部构造函数的组件提供自定义组件激活器,也可以使用例如factory来激活组件:

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>();

但是,您应该首先真正考虑将.ctor内部化。这样做绝对不是一个好主意,尤其是当您将类作为组件暴露在外界的情况下。

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.

这篇关于温莎城堡:向内部构造函数注册类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆