是否有可能通过任意的方法组作为参数的方法? [英] Is it possible to pass an arbitrary method group as a parameter to a method?

查看:102
本文介绍了是否有可能通过任意的方法组作为参数的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写出像下面的

// The type 'MethodGroup' below doesn't exist.  This is fantasy-code.
public void MyFunction(MethodGroup g)
{
    // do something with the method group
}

后来,我可以打电话给的MyFunction 的方法组。事情是这样的。

The later, I could call MyFunction with any method group. Something like this.

MyFunction(Object.Equals)

如果我承诺签名,那么事情做工精细。

If I commit to a signature, then things work fine.

public void MyFunction(Func<object, object, bool> f)
{
    // do something with known delegate
}
...
MyFunction(Object.Equals)

方法组的Object.Equals 是愉快地被迫已知的委托类型 Func键<对象,对象,布尔> ,但我不想承诺一个特定的签名。我想任何方法组传递给的MyFunction

The method group Object.Equals is happily coerced into the known delegate type Func<object, object, bool>, but I don't want to commit to a particular signature. I'd like to pass any method group to MyFunction.

方法组无法转换为 System.Object的

public void MyFunction(object o)
{
    // do something with o
}
...
MyFunction(Object.Equals) // doesn't work

我认为每个人都被遗忘的一个方法调用牙套并在某些时候发现了这一点。我希望,这并不意味着该方法基团是不(或不能被转换),以第一类对象。

I think that everyone's forgotten braces on a method call and discovered this at some point. I'm hoping that this doesn't mean that method groups aren't (or can't be converted) to first class objects.

我不认为LINQ表达式会给我要找的那种一般性的,但我肯定会失去了一些东西。

I don't think that Linq expressions will give the kind of generality I'm looking for, but I could certainly be missing something.

我还要提到的是,如果该方法组包含重载这将是罚款,只要我有检查方法组的一种方式。

I should also mention that it would be fine if the method group contained overloads, provided I have a way of inspecting the method group.

我会用一个方法组办? 我可以打印的所有组中的方法,所有的签名(过载,扩展方法等),我也可以援引的组一些参数(有它解析为正确的超载组如果可能的话)。还有其他的方法做这些事情,但他们可能是您想用的方法组,做一些事情。

由于几个人都提到,我可以接受代表,并转换为特定的已知委托类型,当我打电话的MyFunction

As several people have mentioned, I can accept a Delegate, and cast to a particular known delegate type when I call MyFunction.

public void MyFunction(Delegate d)
{
    // do something with d
}
...
MyFunction((Func<object, object, bool>)Object.Equals)

但是,这并不完全一样传递整个方法组。此选择来自组中的一个方法,并将其转换为一个特定的代理。我真的想通过整个集团一炮打响

But this isn't quite the same as passing the entire method group. This selects one method from the group and converts it to a particular delegate. I would really like to pass the whole group in one shot.

推荐答案

您可以通过一个委托:

public void MyFunction(Delegate d)
{
    // do something with the method group
}

然而,你必须指定调用方法时委托类型:

However you will have to specify the delegate type when calling the method :

MyFunction(new Func<object, object, bool>(Object.Equals));



注:上面的代码是不及格的方法组,但一个单一的方法......我不认为这不是可能通过一个方法组。你为什么要那样做?

Note : the code above is not passing a method group but a single method... I don't think it is possible to pass a method group. Why would you want to do that ?

这篇关于是否有可能通过任意的方法组作为参数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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