public class SomeClass{ public void SomeMethod() { Stackframe frame = new Stackframe(1); var method = frame.GetMethod(); var type = method.DeclaringType; var name = method.Name; }}现在,假设您有另一个这样的课程:
public class Caller{ public void Call() { SomeClass s = new SomeClass(); s.SomeMethod(); }}名称将是“ Call”,类型将是“ Caller”
更新两年后,我仍然对此表示赞同
在.Net
4.5中,现在有一种更简单的方法来执行此操作。您可以利用
CallerMemberNameAttribute
继续前面的示例:
public class SomeClass{ public void SomeMethod([CallerMemberName]string memberName = "") { Console.WriteLine(memberName); //output will be name of calling method }}

![如何使用反射获取调用方法的名称和类型?[重复] 如何使用反射获取调用方法的名称和类型?[重复]](http://www.mshxw.com/aiimages/31/506786.png)
