gpt4 book ai didi

c# - 实现内部类内部接口(interface)报错

转载 作者:行者123 更新时间:2023-11-30 14:12:16 25 4
gpt4 key购买 nike

为什么我不能这样做?

internal class InsuranceClientFactory : IInsuranceClientFactory
{
internal Iws2SoapClient InsuranceClient()
{

}
}

internal interface IInsuranceClientFactory
{
Iws2SoapClient InsuranceClient();
}

我得到错误:

Cannot implement an interface member because it is not public

有几个问题类是公开的但接口(interface)不是,但我的都是内部的。我不想在程序集之外公开此类或接口(interface)。似乎是一个奇怪的限制。

这个问题C# internal interface with internal implementation , 是否声明 “如果您正在隐式实现一个接口(interface),我相信该成员必须声明为公共(public)的。” 但这对我来说没有意义,为什么你不能?似乎有道理?

最佳答案

“我不想暴露这个类或接口(interface)……”那很好。

但是该方法需要是公开的,即使它是公开的也不会在程序集外可见,因为该类是内部的。

如果您真的真的不想公开它,您可以显式实现该方法。

internal class InsuranceClientFactory : IInsuranceClientFactory
{
Iws2SoapClient IInsuranceClientFactory.InsuranceClient()
{

}
}

关于c# - 实现内部类内部接口(interface)报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18129110/

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