有下面一个类,和属性public class ClassName
    {
        public static string aaa
        {
            get {
                return ""; //这里怎么写?
            }
        }
    }我希望在程序中使用 ClassName.aaa可以得到 ClassName,当然ClassName是可变的.

解决方案 »

  1.   

    To:cnhgj(戏子)(黄某人养不成沙皮狗)
    Keyword 'this' is not valid in a static property, static method, or static field initializer
      

  2.   


    http://community.csdn.net/Expert/topic/3525/3525570.xml?temp=.9516565
    不是一样的吗?这样就可以得到当前类的名字了,而不管你这个类是父类还是子类都能正确的返回当前的类的名字:
    public class ClassName
        {
            public static string aaa
            {
                get {
                    return this.GetType().FullName; //这里怎么写?
                }
            }
        }
      

  3.   

    是这样啊,你为什么要写成static的呢?
      

  4.   

    不要用静态方法
    public class ClassName
        {
            public string aaa
            {
                get {
                    return this.ToString();; //这里怎么写?
                }
            }
        }
      

  5.   

    To:hbxtlhx(下着春雨的天) 是因一些特殊的需要,如果没有static一切就简单的多.目前我倒是有一个方法,可是很麻烦,可操作性不强.我想看看有没有更简单的方法
      

  6.   

    好好想想吧,static里怎么会有this?public class ClassName
        {
            public static string aaa
            {
                get {
                    return System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name;
                }
            }    }
      

  7.   

    不要用静态方法,就是这么写了:public class ClassName
        {
            public string aaa
            {
                get
                {
                    return this.GetType().FullName; //这里怎么写
                }
            }
        }
      

  8.   

    请思归在http://community.csdn.net/Expert/topic/3525/3525570.xml?temp=.3554041
    上领分.
      

  9.   

    yes思归说的对
    this是对引用类的当前实例,而静态方法属于类本身,不能被实例化引用