1. Introduction
protected internal
은멤버 액세스 한정자
이다.선언 어셈블리
또는파생된 형식
에서 액세스 가능하며, 이외 영역에서는 접근이 불가능하다.
2. Information
2.1. protected
protected의 범위는 선언 및 파생 클래스 내부로, 클래스 외부에서는 접근이 불가하다.
이를 다이어그램 및 코드로 나타내면 아래와 같다.
// Assembly 1 namespace Assembly1 { public class BaseClass { protected string A = "protected"; } internal class DerivedA : BaseClass { public DerivedA() { A = "DerivedA"; // OK } } internal class OtherClass { public BaseClass Base = new BaseClass(); public OtherClass() { Base.A = "OtherClass"; // CS0122 } } }
// Assembly 2 namespace Assembly2 { internal class DerivedB : BaseClass { public DerivedB() { A = "DerivedB"; // OK } } internal class OtherClass { public BaseClass Base = new BaseClass(); public OtherClass() { Base.A = "OtherClass"; // CS0122 } } }
2.2. internal
internal의 범위는 해당 어셈블리 내로, 어셈블리 안에서는 접근이 자유롭다.
이를 다이어그램 및 코드로 나타내면 아래와 같다.
// Assembly 1 namespace Assembly1 { public class BaseClass { internal string A = "internal"; } internal class DerivedA : BaseClass { public DerivedA() { A = "DerivedA"; // OK } } internal class OtherClass { public BaseClass Base = new BaseClass(); public OtherClass() { Base.A = "OtherClass"; // OK } } }
// Assembly 2 namespace Assembly2 { internal class DerivedB : BaseClass { public DerivedB() { A = "DerivedB"; // CS0122 } } internal class OtherClass { public BaseClass Base = new BaseClass(); public OtherClass() { Base.A = "OtherClass"; // CS0122 } } }
3. protected internal
앞서
protected internal
은`선언 어셈블리` 또는 `파생된 형식`에서 액세스 가능하며, 이외 영역에서는 접근이 불가능하다.
라고 표현하였다.여기서
또는
이라는 표현이 중요한데, 결론적으로protected
와internal
접근 가능 범위의 합집합으로 적용된다.
// Assembly 1 namespace Assembly1 { public class BaseClass { protected internal string A = "protected internal"; } internal class DerivedA : BaseClass { public DerivedA() { A = "DerivedA"; // OK } } internal class OtherClass { public BaseClass Base = new BaseClass(); public OtherClass() { Base.A = "OtherClass"; // OK } } }
// Assembly 2 namespace Assembly2 { internal class DerivedB : BaseClass { public DerivedB() { A = "DerivedB"; // OK } } internal class OtherClass { public BaseClass Base = new BaseClass(); public OtherClass() { Base.A = "OtherClass"; // CS0122 } } }