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

标签 c# internal protected

我在同一个程序集中定义了一个接口(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/

相关文章:

c# - 从子母版页代码后面访问子母版页上的用户控件

c# - 从 wpf datagrid 获取隐藏值

javascript - 如何像 C# 一样在 Javascript 中传递手动参数顺序?

c# - Internals、InternalsVisibleTo 和测试共享行为

iOS 分发 - plist 的 itms-services 协议(protocol)链接中的参数

java - 访问父类子类中静态类的 protected 成员

c# - double 在特殊情况下丢失数字(也没有 int 转换)

JavaScript:Object.prototype.toString(new Number(5)) 似乎返回错误的类型

android - 将图像存储在内部存储器中

c++ - 在派生类 C++ 中将基类的 protected 成员访问声明为 public