gpt4 book ai didi

c# - Windsor 为多个接口(interface)注册单例组件

转载 作者:IT王子 更新时间:2023-10-29 04:37:09 27 4
gpt4 key购买 nike

我想在 CaSTLe.Windsor 中注册一个具有 2 个接口(interface)的类。

此代码是否有效...我是否只有一个实例用于两个接口(interface)...

Component.For<IEnvironment>().ImplementedBy<OutlookEnvironment>().LifestyleSingleton()
Component.For<IOutlookEnvironment>().ImplementedBy<OutlookEnvironment>().LifestyleSingleton()

我需要仔细检查一下,因为我的环境应该始终是同一个实例...

因此,当我使用 IEnvironment 接口(interface)解析时,我应该获得与使用 IOutlookEnvironment 解析组件时相同的实例

最佳答案

您需要使用 Component.For 方法的 use multi-generic-parameter 重载

Component.For<IEnvironment, IOutlookEnvironment>()
.ImplementedBy<OutlookEnvironment>()
.LifestyleSingleton()

另请参阅文档:Registering component with multiple services部分。

关于c# - Windsor 为多个接口(interface)注册单例组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14757804/

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