次の方法で共有


protected internal (C# リファレンス)

protected internal キーワードの組み合わせは、メンバー アクセス修飾子です。 保護された内部メンバーは、現在のアセンブリまたは包含クラスから派生した型からアクセスできます。 protected internalと他のアクセス修飾子の比較については、「アクセシビリティ レベル」を参照してください。

基底クラスの保護された内部メンバーには、その包含アセンブリ内の任意の型からアクセスできます。 また、別のアセンブリにある派生クラスでも、派生クラス型の変数を介してアクセスが行われる場合にのみアクセスできます。 たとえば、次のコード セグメントを考えてみます。

// Assembly1.cs
// Compile with: /target:library
public class BaseClass
{
   protected internal int myValue = 0;
}

class TestAccess
{
    void Access()
    {
        var baseObject = new BaseClass();
        baseObject.myValue = 5;
    }
}
// Assembly2.cs
// Compile with: /reference:Assembly1.dll
class DerivedClass : BaseClass
{
    static void Main()
    {
        var baseObject = new BaseClass();
        var derivedObject = new DerivedClass();

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

        // OK, because this class derives from BaseClass.
        derivedObject.myValue = 10;
    }
}

この例には、 Assembly1.csAssembly2.csの 2 つのファイルが含まれています。 最初のファイルには、パブリック 基底クラス、 BaseClass、および別のクラス TestAccessが含まれています。 BaseClass は、保護された内部メンバー ( myValue) を所有します。これは、同じアセンブリ内にあるため、 TestAccess 型によってアクセスされます。 2 番目のファイルでは、myValueのインスタンスを介してBaseClassにアクセスしようとするとエラーが発生しますが、派生クラスのインスタンスを介してこのメンバーにアクセスすると、DerivedClassは成功します。 これは、 protected internal同じアセンブリ内の任意のクラス または 任意のアセンブリ内の派生クラスからのアクセスを許可し、保護されたアクセス修飾子の中で最も許容されることを示しています。

構造体は継承できないため、構造体メンバーはprotected internalにすることはできません。

保護された内部メンバーのオーバーライド

仮想メンバーをオーバーライドする場合、オーバーライドされたメソッドのアクセシビリティ修飾子は、派生クラスが定義されているアセンブリによって異なります。

派生クラスが基底クラスと同じアセンブリで定義されている場合、オーバーライドされたすべてのメンバーは protected internal アクセスできます。 派生クラスが基底クラスとは異なるアセンブリで定義されている場合、オーバーライドされたメンバーは protected アクセスできます。

// Assembly1.cs
// Compile with: /target:library
public class BaseClass
{
    protected internal virtual int GetExampleValue()
    {
        return 5;
    }
}

public class DerivedClassSameAssembly : BaseClass
{
    // Override to return a different example value, accessibility modifiers remain the same.
    protected internal override int GetExampleValue()
    {
        return 9;
    }
}
// Assembly2.cs
// Compile with: /reference:Assembly1.dll
class DerivedClassDifferentAssembly : BaseClass
{
    // Override to return a different example value, since this override
    // method is defined in another assembly, the accessibility modifiers
    // are only protected, instead of protected internal.
    protected override int GetExampleValue()
    {
        return 2;
    }
}

C# 言語仕様

詳細については、C# 言語仕様のを参照してください。 言語仕様は、C# の構文と使用法の決定的なソースです。

こちらも参照ください