public class A
{
    string name;
    public string Name
    {
        get{return this.name;}
        set{this.name=value;}
    }
}我通过什么方法在我调用Name属性时可以取到"Name"字符串呢?

解决方案 »

  1.   

    没明白有什么用你都调用name了,就直接写个字符串不就得了么??得到类的属性的名称可以用反射PropertyInfo
      

  2.   

    PropertyDescriptorCollection pc=TypeDescriptor.GetProperties(xtv);
    foreach(PropertyDescriptor pd in pc)
    {
    string sv="null";
    if(pd.GetValue(xtv)!=null)
    sv=pd.GetValue(xtv).ToString();
    richTextBox1.AppendText(pd.Name+":"+sv+"\n");
    }
    //其中xtv是我的自定义控件
    这样不仅得到属性名,也可以得到属性值
      

  3.   

    也可以用返射:
    System.Reflection.PropertyInfo[] pis=xtv.GetType().GetProperties();
    richTextBox1.Clear();
    foreach(System.Reflection.PropertyInfo pi in pis)
    {
    string sv="null";
    if(pi.GetValue(xtv,(object[])null)!=null)
    sv=pi.GetValue(xtv,(object[])null).ToString();
    richTextBox1.AppendText(pi.Name+":"+sv+"\n");
    }