是否可以不使用手动编写的字符串获取同一类中另一个方法的名称?
class MyClass {
private void doThis()
{
// Wanted something like this
print(otherMethod.name.ToString());
}
private void otherMethod()
{
}
}
你可能会问为什么:好吧,原因是我必须稍后调用这个方法,就像这个 Invoke("otherMethod"),但是我不想自己硬编码这个字符串,因为我不能再在项目。
最佳答案
一种方法是将其包装到委托(delegate)Action
中,然后您可以访问方法的名称:
string name = new Action(otherMethod).Method.Name;
关于c# - 如何从已知方法中获取方法名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18040116/