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.cs 과 Assembly2.cs.
첫 번째 파일에는 공용 기본 클래스 BaseClass와 다른 클래스가 TestAccess포함됩니다.
BaseClass는 동일한 어셈블리에 있기 때문에 myValue라는 보호된 내부 멤버를 소유하고 있으며, TestAccess 형식에서 이를 액세스합니다.
두 번째 파일에서 인스턴스 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# 구문 및 사용의 최종 소스입니다.
참고하십시오
.NET