简体   繁体   English

DataRow 和受保护的内部构造函数

[英]DataRow and the protected internal constructor

How can a DataTable create a new instance of a DataRow with the NewRow method if the constructor of the DataRow class is protected internal and DataTable doesn't inherit from DataRow?如果 DataRow class 的构造函数在内部受保护并且 DataTable 不从 DataRow 继承,DataTable 如何使用 NewRow 方法创建 DataRow 的新实例?

Example:例子:

class Program
{
    static void Main()
    {
        // error: inaccessible due to its protection level
        DataRow dr = new DataRow(); 

        // works
        DataRow dr = new DataTable().NewRow();
    }
}

protected internal means "accessible by derived classes" and "accessible by other classes in the same assembly". protected internal意味着“派生类可访问”和“同一程序集中的其他类可访问”。 DataTable and DataRow are in the same assembly, so DataTable has access to all of DataRow 's internal members. DataTableDataRow在同一个程序集中,因此DataTable可以访问DataRow的所有内部成员。

Hope you already got an answer for this.希望你已经得到了答案。

But still I am adding my answer to this to address “Why it is designed in this way”.但我仍然在添加我的答案来解决“为什么它是这样设计的”。

As “hvd” mentioned they are in same assembly that's why DataTable able to create instance of DataRow.正如“hvd”所提到的,它们在同一个程序集中,这就是 DataTable 能够创建 DataRow 实例的原因。

The reason for this approach is:采用这种方法的原因是:

• Data row contains values for each column • Ideally an array internally used to store these values • 数据行包含每列的值 • 理想情况下是内部用于存储这些值的数组

• So each data row contains array which contains values • 因此每个数据行都包含包含值的数组

• But data row will not be knowing the size of the array to initialize • 但是数据行不知道要初始化的数组的大小

• Which depends on number of columns in the data.table • 这取决于 data.table 中的列数

• But data.table knows how many columns in the table • 但是data.table知道表中有多少列

• That's why it takes responsibility to create OR set the array size of the DataRow • 这就是它负责创建或设置 DataRow 的数组大小的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM