受保护的成员可见用户
问题描述:
这将是我的第一个问题,所以请宽大。受保护的成员可见用户
这怎么可能:
//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 (...)";
}
}
为什么我得到的,因为Form1中访问受保护的成员不从SomeTableTableAdapter继承?
答
的Adapter
属性声明为protected internal
,这意味着它是派生类(protected
)和在同一组件(internal
)类访问。由于Form1
与SomeTableTableAdapter
在同一个程序集中,因此它们可以访问彼此的内部成员。
答
protected internal
表示受保护的或内部的。可以从派生类或包含程序集访问。
Access Modifiers (C# Programming Guide):
保护内部
类型或构件可以通过在其声明,或从另一个组件中的导出类中的组件中的任何代码访问。从另一个程序集进行访问必须在派生自声明受保护内部元素的类的类声明中进行,并且必须通过派生类类型的实例进行。
请使用网站功能回复答案,如发表评论。由于这是一个问答网站,其实答案并不是[答案]将被删除](http://stackoverflow.com/faq#deletion)。不要忘记标记答案已被接受。 – GSerg 2012-08-11 16:02:28