Protected member visible for user

穿精又带淫゛_ 提交于 2019-12-12 18:25:24

问题


It will be my first question here so please be lenient.

How is this possible:

//there is a Form1 class which has a TableAdapter member generated by designer...
partial class Form1
{
    private void InitializeComponent()
    {
         this.SomeTableTableAdapter = new SomeDatabaseDataSetTableAdapters.SomeTableTableAdapter();
    }

    private SomeDatabaseDataSetTableAdapters.SomeTableTableAdapter SomeTableTableAdapter;
 }

//here is this TableAdapter class
//It has PROTECTED member called "Adapter"
public partial class SomeTableTableAdapter : global::System.ComponentModel.Component
{
    protected internal global::System.Data.SqlClient.SqlDataAdapter Adapter
    {
    }
}

//and in the constructor of Form1 class I can do something like this:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.SomeTableTableAdapter.Adapter.InsertCommand.CommandText = @"INSERT INTO (...)";
    }
}

How come I get access to protected member since Form1 do not inherit from SomeTableTableAdapter?


回答1:


The Adapter property is declared as protected internal, which means it is accessible to derived classes (protected) and to classes in the same assembly (internal). Since Form1 is in the same assembly as SomeTableTableAdapter, they can access each other's internal members.




回答2:


protected internal means protected OR internal. Access is allowed either from derived classes or from the containing assembly.

Access Modifiers (C# Programming Guide):

protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.



来源:https://stackoverflow.com/questions/11915651/protected-member-visible-for-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!