6

Is it possible to get the name of another method in the same class but without using a manually written string?

class MyClass {

    private void doThis()
    {
        // Wanted something like this
        print(otherMethod.name.ToString());
    }   

    private void otherMethod()
    {

    }
}

You may ask why: well the reason is that I must invoke the method later on like this Invoke("otherMethod"), however I don't want to hardcode this string myself as I can't refactor it anymore within the project.

Mat
  • 202,337
  • 40
  • 393
  • 406
Malvin
  • 859
  • 2
  • 11
  • 19

4 Answers4

8

One approach is you can wrap it into delegate Action, then you can access the name of method:

string name = new Action(otherMethod).Method.Name;
cuongle
  • 74,024
  • 28
  • 151
  • 206
  • 1
    Thanks this works! Damn I looked in GetType() and GetMethods() but this "new Action" I never heard of. Small question: when I use the new Action I hope the method within does not get executed (it only takes the name), is that correct? – Malvin Aug 04 '13 at 06:02
  • 2
    If you go with delegate approach you don't need to get name (and hence skipping invoke via reflection) - just use resulting `Action` to execute method later. – Alexei Levenkov Aug 04 '13 at 06:02
  • 2
    Woah! Pretty awesome, I didn't know that you can do that. – Dimitar Dimitrov Aug 04 '13 at 06:04
  • @Malvin: Yes, you're right, it's kind of creating a new pointer to point to your method, it does not execute – cuongle Aug 04 '13 at 06:04
  • 1
    @Malvin: You can also use [expression templates](http://stackoverflow.com/q/1213862/541686) to achieve a similar goal with fields/properties/etc., although Cuong's solution is better for methods here. – user541686 Aug 04 '13 at 06:12
2

You can use reflection (example - http://www.csharp-examples.net/get-method-names/) to get the method names. You can then look for the method that you're looking for by name, parameters or even use an attribute to tag it.

But the real question is - are you sure this is what you need? This looks as if you don't really need reflection, but need to think over your design. If you already know what method you're going to invoke, why do you need the name? How about a using a delegate? Or exposing the method via an interface and storing a reference to some class implementing it?

Vadim
  • 2,847
  • 15
  • 18
1

Try this:

MethodInfo method = this.GetType().GetMethod("otherMethod");
object result = method.Invoke(this, new object[] { });
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
0

Btw. I also found (in the expansions of the internet) an alternative solution for only getting the string of a method. It also works with parameters and return types:

System.Func<float, string> sysFunc = this.MyFunction;
string s = sysFunc.Method.Name; // prints "MyFunction"

public string MyFunction(float number)
{
    return "hello world";
}
Malvin
  • 859
  • 2
  • 11
  • 19
  • The only difference is, that this also works on non-void methods. – Malvin Aug 26 '13 at 20:13
  • The approach is identical, you'd obviously need to use the `Action`/`Func` type that matches the signature of the method. The example in the OP is a parameter-less void function, meaning `Action`. You *couldn't* use `Func` for such a method. – Servy Aug 26 '13 at 20:15
  • You are right, it's based on the same roots. I just added it for a reminder as I'm not that professional in C# yet. – Malvin Aug 26 '13 at 20:21