I'd like write a function like the following
// The type 'MethodGroup' below doesn't exist. This is fantasy-code. public void MyFunction(MethodGroup g) { // do something with the method group }
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)
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
.
Method groups cannot be converted to 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.
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.
What would I do with a method group? I could print all the signatures of all the the methods in the group (overloads, extension methods etc), or I could 'invoke' the group with some arguments (have it resolve to the correct overload in the group if possible). There are other ways to do these things but they are some things you might want to do with a method group.
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.
In C#, we can also pass a method as a parameter to a different method using a delegate. We use the delegate keyword to define a delegate. Here, Name is the name of the delegate and it is taking parameter.
Parameters act as variables inside the method. They are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma. The following example has a method that takes a string called fname as parameter.
Parameter is variable defined in function declaration. Argument is the actual value of this variable that get passed to the function .
ECMA 334v4 §14.1: A method group can be used in an invocation-expression (§14.5. 5), used in a delegate-creation-expression (§14.5. 10.3), or implicitly converted to a compatible delegate type. In any other context, an expression classified as a method group causes a compile-time error.
I think an important question here is: what would you do with a method group if you could ever be passed one?
Keep in mind, a method group is just a name for a set of methods - there may be one or more - and they may be overloads or extension methods. Method groups are not a concept that is preserved in compiled code - the compiler supports conversions from method groups to delegates - but in order for this to be possible the compiler must be able to resolve without any ambiguity exactly what method you intend to pass.
Method groups are only valid as parameters to functions if the function clearly defines the signature of the method. So, for example: public void MyFunction( Func<int> f )
can be passed a method group.
The closest you can come is to write a function that accepts the type Delegate:
public void MyFunction( Delegate d ) { ... }
but you will still not be able to pass method groups in because there is no conversion from method group to Delegate. You will have to cast to a particular delegate signature:
// call MyFunction with a particular delegate MyFunction( (Func<string>)myObj.ToString ); // method group conversion may occur
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With