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

2020/1/20 10:24:22

Category 软件技术 Tag c#,反映

Type t = typeof(System.Drawing.Color);

string className = t.Name;

MessageBox.Show(className);

//获取所有方法

System.Reflection.MethodInfo[] methods = t.GetMethods();

string str="";

foreach (System.Reflection.MethodInfo method in methods)

{

str += method.Name + System.Environment.NewLine;

}

//获取所有成员

System.Reflection.MemberInfo[] members = t.GetMembers();


//获取所有属性

System.Reflection.PropertyInfo[] properties = t.GetProperties();

foreach (System.Reflection.PropertyInfo property in properties)

{

str += property.Name + System.Environment.NewLine;

}