protected internal(C# 참조)

protected internal 키워드 조합은 멤버 액세스 한정자입니다. protected internal 멤버는 포함하는 클래스에서 파생된 형식이나 현재 어셈블리에서 액세스할 수 있습니다. 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의 두 파일이 포함되어 있습니다. 첫 번째 파일은 공용 기본 클래스인 BaseClass와 다른 클래스인 TestAccess을 포함합니다. BaseClassTestAccess 형식으로 액세스되는 protected internal 멤버인 myValue를 소유합니다. 두 번째 파일에서 BaseClass의 인스턴스를 통한 myValue 액세스의 시도는 오류를 생성하지만 파생된 클래스 DerivedClass의 인스턴스를 통한 이 멤버로의 액세스는 성공합니다.

구조체를 상속할 수 없기 때문에 구조체 멤버는 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# 구문 및 사용법에 대 한 신뢰할 수 있는 소스 됩니다.

참고 항목