有文档的受保护属性在框架类中的用途是什么?

时间:2011-07-21 15:51:32

标签: c#

DataGridViewRowCollection具有以下属性:

Count (public)
Gets the number of rows in the collection.

DataGridView (protected)
Gets the DataGridView that owns the collection.

Item (public)
Gets the DataGridViewRow at the specified index.

List (protected)
Gets an array of DataGridViewRow objects.

这个问题突然出现在我的脑海中:我将如何使用这些受保护的成员?我假设他们在文档中占用空间是有原因的。

我认为可以使用的场景是派生我自己的类并告诉DataGridView使用该类。

然而,我不知道该怎么做(也许这很明显,我只是没有看到 - 这就是答案)。

3 个答案:

答案 0 :(得分:3)

只能从更加派生的类访问受保护的项目。这意味着,如果您创建的MyDataGridViewRowCollection继承自DataGridViewRowCollection,则可以访问DataGridViewList属性。

要创建一个继承自DataGridViewRowCollection的类,您只需执行以下操作:

public class MyDataGridViewRowCollection : DataGridViewRowCollection
{
    public void MyMethod ()
    {
    }
}

答案 1 :(得分:1)

如果不查看您引用的受保护属性的内部工作方式,您的假设是正确的:您可以从DataGridViewRowCollection派生来实现您自己的某些特殊功能。

我认为您还必须将DataGridView重写为实例,并使用您的DataGridViewRowCollection代替......

答案 2 :(得分:1)

DataGridViewRowCollection未密封,因此您可以继承它。在这种情况下,您可以调用这些方法,因此他们有文档。

相关问题