C#扩展索引器? [英] C# extend indexer?
本文介绍了C#扩展索引器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用扩展方法来扩展索引器?我希望能够使用这样的标题文本从给定的DataGridViewRow
的DataGridViewCell
中获取单元格的值:
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屋!
查看全文