C# 利用反射,遍历获得一个类的所有属性名,方法名,成员名.

using System.Reflection;

     private void button1_Click(object sender, EventArgs e)
     {
         Type t = typeof(System.Drawing.Color);
         string className = t.Name;
         MessageBox.Show(className);
         //获取所有方法
         System.Reflection.MethodInfo[] methods = t.GetMethods();
         this.textBox1.Text = "";
         foreach (System.Reflection.MethodInfo method in methods)
         {
            this.textBox1.Text += method.Name + System.Environment.NewLine;
         }
         //获取所有成员
          System.Reflection.MemberInfo[] members = t.GetMembers();

         //获取所有属性
         System.Reflection.PropertyInfo[] properties = t.GetProperties();
         foreach (System.Reflection.PropertyInfo property in properties)
         {
             this.lstColors.Items.Add(property.Name);
         }
       }

你可能感兴趣的:(c#)