C# 扩展索引器? [英] C# extend indexer?

查看:27
本文介绍了C# 扩展索引器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用扩展方法来扩展索引器?我希望能够使用如下标题文本从给定 DataGridViewRowDataGridViewCell 获取单元格的值:

Is it possible to use extension methods to extend an indexer? I want to be able to get cell's value from DataGridViewCell of given DataGridViewRow using header text like this:

object o = row["HeaderText"]; 

我有这个代码

public static class Ext
{
    public static object Cells(this DataGridViewRow r, string header)
    {
        foreach (DataGridViewCell c in r.Cells)
        {
            if (c.OwningColumn.HeaderText == header)
            {
                return c.Value;
            }
        }
        return null;
    }
}

我想要类似的索引器.谢谢.

and I want similar indexer. Thanks.

推荐答案

索引器实际上是属性,C# 中不存在扩展属性.所以这不能按照你想要的方式完成.

Indexers are actually properties, and extension properties do not exist in C#. So this can't be done the way you want.

请参阅这篇博文 了解有关该主题的一些背景知识,并解释为何考虑该功能,但最终从 C# 3.0 中省略.

See this blog post for some background on the subject, and an explanation as to why that feature was considered, but ultimately omitted from C# 3.0.

这篇关于C# 扩展索引器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆