加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 实现内部接口错误的内部类

发布时间:2020-12-16 01:56:39 所属栏目:百科 来源:网络整理
导读:为什么我不能这样做? internal class InsuranceClientFactory : IInsuranceClientFactory{ internal Iws2SoapClient InsuranceClient() { }}internal interface IInsuranceClientFactory{ Iws2SoapClient InsuranceClient();} 我收到错误: Cannot implemen
为什么我不能这样做?

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

    }
}

internal interface IInsuranceClientFactory
{
  Iws2SoapClient InsuranceClient();
}

我收到错误:

Cannot implement an interface member because it is not public

有几个问题,班级是公开的,但界面不是,但我的两个都是内部的.我不想在程序集之外公开这个类或接口.似乎有一个奇怪的限制.

这个问题C# internal interface with internal implementation确实说明“如果你隐式实现了一个接口,我相信该成员必须被公开.”但这对我来说没有意义,为什么不能呢?似乎有道理?

解决方法

我可以告诉你为什么它不起作用. C#语言规范5.0第13.4.4节,接口映射.

… Neither non-public nor static members participate in interface mapping …

但是因为您尝试做的事情应该在技术上有效,所以显式实现优先,您的实现可以保持内部

而不是分裂,但即使你明确地实现了内部接口,实现也不会是内部的,它实际上是私有/公共的,如规范前面所述(第13.4.1节显式接口成员实现):

Explicit interface member implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through their fully qualified name in a method invocation or a property access,they are in a sense private. However,since they can be accessed through an interface instance,they are in a sense also public.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读