gpt4 book ai didi

caSTLe-windsor - 温莎城堡 : Register class with internal constructor?

转载 作者:行者123 更新时间:2023-12-01 11:08:55 24 4
gpt4 key购买 nike

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

谢谢琼妮

最佳答案

是的,这是可能的。默认组件激活器只查找公共(public)构造函数。您可以为考虑内部构造函数的组件提供自定义组件激活器,或者使用例如工厂来激活组件:

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

但是,您真的应该首先重新考虑将 .ctor 设为内部。这样做很少是个好主意,尤其是当您无论如何都要将类作为组件暴露给外界时。

关于caSTLe-windsor - 温莎城堡 : Register class with internal constructor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2370546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com