C#反射类中所有字段,属性,方法(转)
可能大家都知道在C#中如何创建一个类,但对于类的结构可能大家不一定了解的很清楚,对于我来说,我之前也搞的不是很明白,今天,当我没事研究反射的时候突然发现了着一点。
我们来看类的结构到底是什么
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class People //类名 { private static string name; //字段 private string sex;//字段 public string Sex //属性 { get { return sex; } set { sex = value; } } public static string Name //属性 { get { return People.name; } set { People.name = value; } } private People() //构造函数 { } public static string GetName() // 函数 { if (string.IsNullOrEmpty(name)) { name = "my name"; } return name; } }
以上所有的构造函数、事件、字段、方法和属性都叫做成员,即Member
运行下列代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Type t = typeof(People); Console.WriteLine("----------------Method------------------"); MethodInfo[] methods = t.GetMethods( ); foreach (MethodInfo method in methods) { Console.WriteLine("Method:" + method); //Console.WriteLine(method); //Console.WriteLine("返回值:" + method.ReturnParameter); } Console.WriteLine("---------------Field-------------------"); FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance|BindingFlags.Static); foreach (FieldInfo field in fields) { Console.WriteLine("Field:" + field); } Console.WriteLine("--------------Member--------------------"); MemberInfo[] members = t.GetMembers(); foreach (MemberInfo member in members) { Console.WriteLine("Member:" + member); } Console.WriteLine("--------------Property--------------------"); PropertyInfo[] properties = t.GetProperties( ); foreach (PropertyInfo property in properties) { Console.WriteLine("Property:" + property); } Console.WriteLine("--------------Constructor--------------------"); ConstructorInfo[] constructors = t.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance); foreach (ConstructorInfo constructor in constructors) { Console.WriteLine("Constructor:" + constructor); }
输出结果为:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
----------------Method------------------ Method:System.String get_Sex() Method:Void set_Sex(System.String) Method:System.String get_Name() Method:Void set_Name(System.String) Method:System.String GetName() Method:System.String ToString() Method:Boolean Equals(System.Object) Method:Int32 GetHashCode() Method:System.Type GetType() ---------------Field------------------- Field:System.String sex Field:System.String name --------------Member-------------------- Member:System.String get_Sex() Member:Void set_Sex(System.String) Member:System.String get_Name() Member:Void set_Name(System.String) Member:System.String GetName() Member:System.String ToString() Member:Boolean Equals(System.Object) Member:Int32 GetHashCode() Member:System.Type GetType() Member:System.String Sex Member:System.String Name --------------Property-------------------- Property:System.String Sex Property:System.String Name --------------Constructor-------------------- Constructor:Void .ctor() 请按任意键继续. . .
另外,我们发现属性Sex和Name编译以后变成了get_Sex,get_Name,set_Sex,set_Name,呵呵,看来还是更java一样
原文出处:http://www.cnblogs.com/shuzhengyi/archive/2010/10/12/1848991.html