在不知道一个类的有多少属性时,用反射机制动态的读取出该类的公共属性?????

解决方案 »

  1.   


    object[] obj11 = typeof(类名).GetCustomAttributes(true);
      

  2.   


    PropertyInfo[] properties = typeof(yourclass).GetProperties();
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Text;namespace WindowsApplication1
    {
        public class MyClass
        {
            public MyClass()
            {
            }        private string _name;
            public String Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }
    }
            private void button3_Click(object sender, EventArgs e)
            {
                MyClass my = new MyClass();
                Type T = my.GetType();
                PropertyInfo[] ps = T.GetProperties();
                for (int i = 0; i < ps.Length; i++)
                {
                    listBox1.Items.Add(ps[i].Name);
                }
            }