在帮助方法/属性中包装MethodBase.GetCurrentMethod()?

时间:2017-01-19 14:53:53

标签: c# reflection

private string MethodName
{
   get
   {
      MethodBase m = MethodBase.GetCurrentMethod();
      return String.Format("{0}.{1}", m.ReflectedType.Name, m.Name);
   }
}

我想做类似的事情但当然现在调用的方式与我想要描述的方法不同。有没有一种简单的方法来解决这个问题或者一个明显的替代方案?

1 个答案:

答案 0 :(得分:2)

你可以从StackTrace做出类似的事情:

string MethodName
{
    get
    {
        return new StackTrace()
                    .GetFrame(1) // Get previous frame because we want to know the calling method name
                    .GetMethod()
                    .Name;
    }
}