Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the methodname from a known method?

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.

like image 234
Malvin Avatar asked Aug 04 '13 05:08

Malvin


1 Answers

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;
like image 145
cuongle Avatar answered Sep 22 '22 00:09

cuongle