public class A
    {
        public int Pro1 { get; set; }
        public int Pro2 { get; set; }
    }A a = new A();
如何获取a.Pro1的字符串名称。即如何获得"Pro1"

解决方案 »

  1.   


    using System.Reflection;
    Type t = typeof(A);
    foreach(PropertyInfo pi in t.GetProperties(BindingFlags.Instance | BidngFlags.Public))
    {
        Console.WriteLine(pi.Name);
    }
      

  2.   

    反射,
    System.Reflection.PropertyInfo[] propertys =对像.GetType().GetProperties();
                    foreach (System.Reflection.PropertyInfo info in propertys)
                    {
                       //info.Name 属性名称
                    }
      

  3.   

    void Test()
            {
                A a = new A();
                Type t = a.GetType();            foreach(PropertyInfo info in t.GetProperties())
                {
                    Console.WriteLine(info.Name); 
                }            Console.ReadLine();
            }
      

  4.   

    a.GetType().GetProperty("Pro1").GetValue(a).ToString()   
      

  5.   


    如果我已经知道是"Pro1"就不需要再获取这个值了啊~
      

  6.   

    我提供的条件就是给定的属性啊~其实是这样的,每个属性都有自己的Attribute,比如Pro1的DefaultValue为1,Pro2的DefaultValue为2,我就想在外部调用的时候获得这个DefaultValue,但是并不是说每次都要Pro1.也许我在class B里用的时候需要Pro1,而class C里却是需要Pro2的。如果用遍历的话也是需要提供其他参数才能满足我的需求的~但是现在的逻辑能够提供的就是a.Pro1不知道有没有说清楚~
      

  7.   

    我好像大概知道你的意思了
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication15
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            A AA = new A();
                AA.Pro1 = 1;
                AA.Pro2 = 2;            B BB = new B();
                BB.Pro1 = 1;
                BB.Pro2 = 2;            MessageBox.Show(((DefaultValueAttribute)(AA.GetType().GetProperty(AA.GetNeededPropName)
                    .GetCustomAttributes(typeof(DefaultValueAttribute), false)[0])).Value.ToString()); //1            MessageBox.Show(((DefaultValueAttribute)(BB.GetType().GetProperty(BB.GetNeededPropName)
                    .GetCustomAttributes(typeof(DefaultValueAttribute), false)[0])).Value.ToString()); //4
            }        class X
            {
                virtual public String GetNeededPropName { get; set; }
            }        class A : X
            {
                [DefaultValue(1)]
                public int Pro1 { get; set; }            [DefaultValue(2)]
                public int Pro2 { get; set; }            public override string GetNeededPropName
                {
                    get
                    {
                        return "Pro1";
                    }
                }
            }        class B : X
            {
                [DefaultValue(3)]
                public int Pro1 { get; set; }            [DefaultValue(4)]
                public int Pro2 { get; set; }            public override string GetNeededPropName
                {
                    get
                    {
                        return "Pro2";
                    }
                }
            }
        }
    }
      

  8.   

    get 和set貌似是C#里才有的
      

  9.   

    wartim
    非常感谢你的回复~但是我不得不说这不是我想要的结果~
    按照你的方法~这个类就只能返回一个名称,而我想要的是根据属性来返回这个属性的名称~
    比如Pro1就返回“Pro1”,Pro2就返回"Pro2",而不是在代码中硬编码~
      

  10.   

    你这个需求有点变态,把编程理想化了。 除非那天Microsoft 能发明一个什么属性动态委托之类的,才能解决你的问题。
      

  11.   


    听不懂了,是不是返回defaultvalue特征设置的那个属性名?[defaultvalue(1)]
    pro1pro2返回pro1?或者你写段伪代码说明你的思路
      

  12.   

    我想我明白lz的意思了。lz是希望根据类中某一个成员的地址,获得它在类中的名字。不过在c#中,给定方法名字,是要求编译器取得方法的地址,给定属性名字,却是要求编译器取得属性的值,这两者的处理方式是不同的。
    如果说一定要有一个办法来解决,我只想到一个运算符:& 也就是取变量地址运算符,这是unsafe的,但也许对lz有用。不过我还是没有办法给出一个满足lz要求的解决方案来
      

  13.   

    另外,如#32楼所说的:“没什么特征怎么才能知道你想要它?”,给属性加特征也是可以考虑的,只要在设计类的时候,给所有属性加上Attribute就好了,在反射的威力下,可以区别每一个不同的成员。但还是不能解决lz的问题
      

  14.   

    其实我是想获取给定某个属性的DefaultValue  就是这样的需求
      

  15.   

    class Program
        {
            static void Main(string[] args)
            {
                A a = new A(10,20);
                Console.WriteLine(a.GetPropertyName(a,a.Pro1));
            }
        }    public class A
        {
            public A(int pro1, int pro2)
            {
                Pro1 = pro1;
                Pro2 = pro2;
            }        public int Pro1 { get; set; }
            public int Pro2 { get; set; }        public string GetPropertyName(A a, object value)
            {
                foreach (PropertyInfo info in this.GetType().GetProperties())
                {
                    if (info.GetValue(a, null).ToString() == value.ToString())
                    {
                        return info.Name;
                    }
                }
                return string.Empty;
                //throw new Exception("Not Found This Property");
            }
        }
      

  16.   

     class Program
        {
            static void Main(string[] args)
            {
                A a = new A(10,20);
                Console.WriteLine(A.GetPropertyName(a,a.Pro1));
            }
        }    public class A:PropertyEnmunble<A>
        {
            public A(int pro1, int pro2)
            {
                Pro1 = pro1;
                Pro2 = pro2;
            }        public A()
            {
                Pro1 = -1;
                Pro2 = -2;
            }        public int Pro1 { get; set; }
            public int Pro2 { get; set; }
        }    public class PropertyEnmunble<T>
        {
            public static string GetPropertyName(T a, object value)
            {
                foreach (PropertyInfo info in a.GetType().GetProperties())
                {
                    if (info.GetValue(a, null).ToString() == value.ToString())
                    {
                        return info.Name;
                    }
                }
                return string.Empty;
            }
        }
      

  17.   

    楼上的方法还是有问题的!~
    使用这个方法的前提是实体中属性的值都得不一样~否则就不对了~
      
    比如
    static void Main(string[] args)
            {
                A a = new A(20, 20);
                Console.WriteLine(A.GetPropertyName(a, a.Pro1));
                Console.WriteLine(A.GetPropertyName(a, a.Pro2));
                Console.ReadLine();
            }两次输出都是Pro1
    但是我想要的结果是输出Pro1和Pro2
      

  18.   

    class 类
    {
    putlic static readonly string ReadID="ID";
    public static readonly string ReadContetn="Content";public int ID{get;set;}
    public string Content {get;set;}}其实他想要的是这种效果,使用Read字段可以获得属性所对应的名称,但是以静态字段的方法没办法同步更新,也就是说属性改了要是没改字段就会出问题,这种问题不是编译其间可以预防的,所以他想要的结果是在编译的时候就能提示这个字段不是这个类的,我也在考虑着问题,当确实没有好的解决办法
      

  19.   


    public class A
        {
            public int PropA { get; set; }        public string PropB { get; set; }
        }    class Program
        {
            static void Main(string[] args)
            {
                var s = GetPropName<A>(a => a.PropB);
                Console.WriteLine(s);
            }        static string GetPropName<T>(Expression<Func<T, object>> expr)
            {
                switch (expr.Body.NodeType)
                {
                    case ExpressionType.MemberAccess:
                        return ((MemberExpression)expr.Body).Member.Name;
                    case ExpressionType.Convert:
                        return ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
                    default:
                        return null;
                }
            }    }