1.C#4个修饰符的权限
修饰符    级别    适用成员        解释
public    公开    类及类成员的修饰符   对访问成员没有级别限制
private     私有    类成员的修饰符     只能在类的内部访问
protected   受保护的    类成员的修饰符        在类的内部或者在派生类中访问,不管该类和派生类是不是在同一程序集中
internal    内部的     类和类成员的修饰符      只能在同一程序集(Assembly)中访问


protected internal 受保护的内部:如果是继承关系,不管是不是在同一程序集中都可以访问;如果不是继承关系只能在同一程序集中访问

private protected访问限于包含类或当前程序集中派生自包含类的类型。 自 C# 7.2 之后可用。  //编译器会提示多个保护修饰符

访问权限权重:public>protected internal>protected>internal>private protected>private

 

 C# 成员访问修饰符protected internal等_修饰符

 

 

 

 

 C# 成员访问修饰符protected internal等_修饰符_02

 

 

 

 

 

 

 

以下是 private internal用法详细讲解

 
在解决方案ConsonApp1有两个项目:  CsLearnTool、School 。生成两个程序集CsLearnTool.dll、School.dll

 

School.dll 程序集

using System;

namespace School
{

    public class Test
    {

        static void Main(string[] args)
        {

        }
    }
    public class People
    {
        private protected string myName = "";
    }


    
    public class Student : People
    {
        void Access()
        {
            var baseObject = new People();

            //错误 CS1540 无法通过“People”类型的限定符访问受保护的成员“People.myName”
            // classes Student from People.
            baseObject.myName = "小红";

            // 正确
            myName = "liming";
        }
    }
}

 

 

CsLearnTool.dll程序集

using System;
using System.Reflection;
using System.ComponentModel;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using School;

namespace CsLenrnTool
{
    class CsLenrnTool
    {
        static void Main(string[] args)
        {

       
       }

        class Teacher : People
        {
            void Access()
            {  
//错误 CS0122 “People.myName”不可访问,因为它具有一定的保护级别
myName = "liming"; } } } }

通过以上案例我们可以得出:在同一个程序集中 子类可以直接使用 父类的中 带有private protected 修饰符的成员 。同一个程序集中 无法通过实例化的方式访问父类中的private protected 修饰符的成员

                                              在不同一个程序集中不能 子类不能 直接使用 父类的中 带有private protected 修饰符的成员

 

 

以下是protected internal用法详解

 

School.dll 程序集

using System;

namespace School
{

    public class Test
    {

        static void Main(string[] args)
        {

        }
    }
    public class People
    {
        protected internal string myName = "";
    }


    
    public class Student : People
    {
        void Access()
        {
            var baseObject = new People();

            // 正确
            baseObject.myName = "小红";

            // 正确
            myName = "liming";
        }
    }
}

 

School.dll 程序集

 

using System;
using System.Reflection;
using System.ComponentModel;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using School;

namespace CsLenrnTool
{
    class CsLenrnTool
    {
        static void Main(string[] args)
        {

        }

        class Teacher : People
        {
            void Access()
            {
                People pl = new People();
                pl.myName = "";//错误    CS1540    无法通过“People”类型的限定符访问受保护的成员“People.myName”
                //正确
                Teacher tl = new Teacher();
                tl.myName = "";
                 myName = "liming";
            }
        }
    }

}

 protected internal 和 protected 的区别

proteced

C# 成员访问修饰符protected internal等_程序集_03

 

C# 成员访问修饰符protected internal等_c#_04

 

 

 

  protected internal

C# 成员访问修饰符protected internal等_程序集_05

 

C# 成员访问修饰符protected internal等_c#_06

 

编程是个人爱好