16

Is it possible to created a method that takes ANY method (regardless of it's parameters) as a parameter? The method would also have a params parameter which then takes all the parameters for the parameter-method.

So basically what I want is something like this:

public void CallTheMethod(Action<int> theMethod, params object[] parameters)

But then for any method, not just for methods that takes an int.

Is something like this possible?

Thanks

8
  • 7
    Well, you could pass the non-specific Delegate, but DynamicInvoke is sloooooowwwwww (relatively speaking) Commented Aug 20, 2013 at 15:00
  • 5
    Just out of curiosity, how would this be more useful than calling the method directly? I must say I'm intrigued though Commented Aug 20, 2013 at 15:00
  • 1
    Out of curiousity, what's the point of this? It seems very error prone. Why not just run the method? Commented Aug 20, 2013 at 15:01
  • This may be what you want: stackoverflow.com/questions/325156/… Commented Aug 20, 2013 at 15:02
  • 1
    @TheOddler Take a look at monotorrent.blogspot.pt/2009/12/…, you might be able to use Expressions to do what you're after. Commented Aug 20, 2013 at 15:15

4 Answers 4

9

It's possible, but not what should be done.

This is what I would do:

public void CallTheMethod(Action toCall)

You might go "huh". Basically, what it lets the user do is this:

CallTheMethod(() => SomeOtherMethod(with, some, other, parameters));

However, if you want it to return a type, it involves generics:

public void CallTheMethod<T>(Func<T> theMethod)

You can put generic constraints on that type, do whatever you want with it, etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Very true, what I'm asking doesn't seem like a good thing to do. Though Unity wants the name of a method when calling an RPC. So I though to write a little wrapper, so I don't have to hard-code the method name. Could you also take a look at the comment on Matten's answer? You seem to be very knowledgeable about c#. Thanks!
8

Yes, with a delegate:

public object CallTheMethod(Delegate theMethod, params object[] parameters)
{
    return theMethod.DynamicInvoke(parameters);
}

But see Marc Gravell's comment on your question :)

Well, you could pass the non-specific Delegate, but DynamicInvoke is sloooooowwwwww (relatively speaking)

2 Comments

I can't seem to get it to work. I get an error: "Cannot convert from 'method group' to 'System.Delegate'". What I have is a method "DoSomething(int number);" And then your method. So I tried "CallTheMethod(DoSomething, 5)" and then I get the error... Am I doing something wrong, or is there a catch I don't know about?
I get the same error. I recommend the answere of @itsnotalie.
3

Yes. You can use DynamicInvoke to call the methods:

Action<int> method1 = i => { };
Func<bool, string> method2 = b => "Hello";

int arg1 = 3;
bool arg2 = true;
//return type is void, so result = null;
object result = method1.DynamicInvoke(arg1);

//result now becomes "Hello";
result = method2.DynamicInvoke(arg2);

A method to do this would become:

object InvokeMyMethod(Delegate method, params object[] args)
{
    return method.DynamicInvoke(args);
}

Comments

0

You can use Delegate and DynamicInvoke to do this, although just like what other people said, it's slow.

If you get error Cannot convert from 'method group' to 'System.Delegate', you may need to cast it when passing the function as argument. For example if you have:

public static void registerMethod(Delegate method, params object[] parameters) {
    var ret = method.DynamicInvoke(parameters);
    // ...
}

public static long myAdditionMethod(int a, int b){
    return a+b;
}

then you can call the function like this:

registerMethod((Func<int, int, long>)myAdditionMethod, 1, 2)

Or you can also use Action<> instead of Func<> if the method's return type is void.

Note: The solution above is not always required. Depending on your C# project configuration, you can just simply call registerMethod(myAdditionMethod, 1, 2) without getting any compilation error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.