private protected(C# 参考)private protected (C# Reference)

private protected 关键字组合是一种成员访问修饰符。The private protected keyword combination is a member access modifier. 仅派生自包含类的类型可访问私有受保护成员,但仅能在其包含程序集中访问。A private protected member is accessible by types derived from the containing class, but only within its containing assembly. 有关 private protected 和其他访问修饰符的比较,请参阅可访问性级别For a comparison of private protected with the other access modifiers, see Accessibility Levels.

备注

private protected 访问修饰符在 C# 版本 7.2 及更高版本中有效。The private protected access modifier is valid in C# version 7.2 and later.

示例Example

仅当变量的静态类型是派生类类型时,可从派生的类型访问基类的私有受保护成员(在其包含程序集中访问)。A private protected member of a base class is accessible from derived types in its containing assembly only if the static type of the variable is the derived class type. 以下面的代码段为例:For example, consider the following code segment:

public class BaseClass
{
    private protected int myValue = 0;
}

public class DerivedClass1 : BaseClass
{
    void Access()
    {
        var baseObject = new BaseClass();

        // Error CS1540, because myValue can only be accessed by
        // classes derived from BaseClass.
        // baseObject.myValue = 5;

        // OK, accessed through the current derived class instance
        myValue = 5;
    }
}
// Assembly2.cs
// Compile with: /reference:Assembly1.dll
class DerivedClass2 : BaseClass
{
    void Access()
    {
        // Error CS0122, because myValue can only be
        // accessed by types in Assembly1
        // myValue = 10;
    }
}

此示例包含两个文件,即 Assembly1.csAssembly2.csThis example contains two files, Assembly1.cs and Assembly2.cs. 第一个文件包含公共基类 BaseClass 及其派生的类型 DerivedClass1The first file contains a public base class, BaseClass, and a type derived from it, DerivedClass1. BaseClass 拥有私有受保护成员 myValueDerivedClass1 尝试以两种方式访问该成员。BaseClass owns a private protected member, myValue, which DerivedClass1 tries to access in two ways. 通过 BaseClass 的实例第一次尝试访问 myValue 时会产生错误。The first attempt to access myValue through an instance of BaseClass will produce an error. 但是,如果尝试在 DerivedClass1 中将其用作继承的成员,则会成功。However, the attempt to use it as an inherited member in DerivedClass1 will succeed.

在第二个文件中,如果尝试将 myValue 作为 DerivedClass2 的继承成员进行访问,会生成错误,因为仅 Assembly1 中的派生类型可以访问它。In the second file, an attempt to access myValue as an inherited member of DerivedClass2 will produce an error, as it is only accessible by derived types in Assembly1.

如果 Assembly1.cs 包含命名为 Assembly2InternalsVisibleToAttribute,则派生类 DerivedClass1 将可以访问 BaseClass 中声明的 private protected 成员。If Assembly1.cs contains an InternalsVisibleToAttribute that names Assembly2, the derived class DerivedClass1 will have access to private protected members declared in BaseClass. InternalsVisibleTo 使 private protected 成员对其他程序集中的派生类可见。InternalsVisibleTo makes private protected members visible to derived classes in other assemblies.

结构成员不能为 private protected,因为无法继承结构。Struct members cannot be private protected because the struct cannot be inherited.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also