如何在代码中获取类名作为静态字符串属性

时间:2013-08-17 08:54:26

标签: c# class static gettype

我想得到这样的东西:

public class myclass()
{
    public static string ClassName { get { return "myclass"; } }        
}

是否有一种通用方法可以将上面代码示例中的字符串“myclass”替换为拥有类的名称? 我知道我们可以非静态方式做到这一点:

    public string ClassName { get { return this.GetType().Name; } }        

但是在静态?

1 个答案:

答案 0 :(得分:2)

class Test
{
       public static string ClassName
       {
          get
          {
             return MethodBase.GetCurrentMethod().DeclaringType.Name;
          }
       } 
}