gpt4 book ai didi

c# - 内部接口(interface)比内部 protected 构造函数*更难*访问?

转载 作者:太空狗 更新时间:2023-10-29 18:16:13 25 4
gpt4 key购买 nike

我在同一个程序集中定义了一个接口(interface)和一个抽象基类:

IFoo.cs:

internal interface IFoo { ... }

基础.cs:

public abstract class Base
{
internal protected Base(IFoo foo) { ... }
}

这会产生以下编译器错误:

CS0051: Inconsistent accessibility: parameter type 'IFoo' is less
accessible than method 'Base.Base(IFoo)'

如果我将 Base 类构造函数设置为仅供内部使用,错误就会消失。由于该类是抽象的,因此向可访问性添加 protected 可能不会完成任何事情......

仍然,我不明白这个错误。 MSDN 将“ protected 内部”定义为

"Access is limited to the current assembly or types derived from the containing class"

与内部 protected 构造函数相比,内部接口(interface) IFoo 如何较少可访问?

最佳答案

This MSDN page将“ protected 内部”定义为(强调原始内容):

The protected internal accessibility level means protected OR internal, not protected AND internal. In other words, a protected internal member can be accessed from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.

换句话说,当前程序集外部派生自 Base 的类型可以访问 Base(IFoo foo) 但他们无法访问 IFoo ,因为它是内部的。因此错误。

关于c# - 内部接口(interface)比内部 protected 构造函数*更难*访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17864205/

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