如何使用具有扩展方法的索引器进行参数和函数调用

时间:2022-09-23 08:09:37

Is it possible to use indexers with extension methods.

是否可以使用带有扩展方法的索引器。

eg. Consider it as an example only.

如。仅以它为例。

    public static object SelectedValue(this DataGridView dgv, string ColumnName)
    {            
        return dgv.SelectedRows[0].Cells[ColumnName].Value;
    }

EDIT

编辑

  1. usage mygrid.SelectedValue("mycol")

    使用mygrid.SelectedValue(“mycol”)

  2. How to use it as an indexer mygrid.SelectedValue["mycol"] rather than above one.

    如何将它用作索引器网格。选择值["mycol"]而不是超过1。

  3. Is it possible to use it like this as well ? mygrid.SelectedValue["mycol"](out somevalue);

    也可以这样使用吗?mygrid。SelectedValue(“mycol”)(somevalue);

What are the syntax of getting this kind of values. Any simple example or link will work.

得到这种值的语法是什么?任何简单的例子或链接都可以。

2 个解决方案

#1


4  

Well, there are two issues here:

这里有两个问题:

  • C# doesn't (by and large) support named indexers1
  • c#大体上不支持名为indexers1的技术
  • C# doesn't support extension properties, so you can't make SelectedValue a property returning something indexable instead
  • c#不支持扩展属性,所以不能将SelectedValue属性设置为返回可索引的属性

So no, the syntax you've specified there won't work. You could get this to work:

不,你指定的语法不能用。你可以让它发挥作用:

mygrid.SelectedValue()["mycol"]

but that's a bit ugly. I'd stick with the method form if I were you.

但这有点丑。如果我是你,我会坚持这个方法。


1 C# 4 supports calling named indexers on COM objects.

c# 4支持在COM对象上调用命名索引器。

#2


0  

Let me try to clarify the usage and intentions of Extension Method.

让我试着澄清扩展方法的用法和意图。

Consider a Extension Method

考虑一个扩展方法

public static bool IsNullOrEmpty(this string source)
{
    return source == null || source == string.Empty;
}

Now you extend your string class with this Extension Method

现在使用这个扩展方法扩展字符串类

var myString = "Hello World";
Assert.AreEqual(myString.IsNullOrEmpty(), false);

This is what .NET does on compilation:

这就是。net对编译所做的:

public static bool IsNullOrEmpty(string source)
{
    return source == null || source == string.Empty;
}

Using our old school

使用我们的老学校

var myString = "Hello World";
Assert.AreEqual(IsNullOrEmpty(myString), false);

Extension method is nothing but a visualization to what we were used to do.

扩展方法只不过是对我们过去所做的事情的一种可视化。

Well, extending indexers could be possible but Microsoft did not think about it.

扩展索引器是可能的,但是微软并没有考虑。

#1


4  

Well, there are two issues here:

这里有两个问题:

  • C# doesn't (by and large) support named indexers1
  • c#大体上不支持名为indexers1的技术
  • C# doesn't support extension properties, so you can't make SelectedValue a property returning something indexable instead
  • c#不支持扩展属性,所以不能将SelectedValue属性设置为返回可索引的属性

So no, the syntax you've specified there won't work. You could get this to work:

不,你指定的语法不能用。你可以让它发挥作用:

mygrid.SelectedValue()["mycol"]

but that's a bit ugly. I'd stick with the method form if I were you.

但这有点丑。如果我是你,我会坚持这个方法。


1 C# 4 supports calling named indexers on COM objects.

c# 4支持在COM对象上调用命名索引器。

#2


0  

Let me try to clarify the usage and intentions of Extension Method.

让我试着澄清扩展方法的用法和意图。

Consider a Extension Method

考虑一个扩展方法

public static bool IsNullOrEmpty(this string source)
{
    return source == null || source == string.Empty;
}

Now you extend your string class with this Extension Method

现在使用这个扩展方法扩展字符串类

var myString = "Hello World";
Assert.AreEqual(myString.IsNullOrEmpty(), false);

This is what .NET does on compilation:

这就是。net对编译所做的:

public static bool IsNullOrEmpty(string source)
{
    return source == null || source == string.Empty;
}

Using our old school

使用我们的老学校

var myString = "Hello World";
Assert.AreEqual(IsNullOrEmpty(myString), false);

Extension method is nothing but a visualization to what we were used to do.

扩展方法只不过是对我们过去所做的事情的一种可视化。

Well, extending indexers could be possible but Microsoft did not think about it.

扩展索引器是可能的,但是微软并没有考虑。