You might all know how to create a class in C #, but for the structure of the class it may not be clear to me that I did not understand it before, and today, when I'm not studying the reflection, I suddenly find a point.
Let's see what the structure of the class is.
Public classPeople//class name { Private Static stringName//Field Private stringSex//Field Public stringSex//Properties { Get{returnsex;} Set{sex =value;} } Public Static stringName//Properties { Get{returnPeople.name;} Set{People.name =value;} } PrivatePeople ()//constructor Function { } Public Static stringGetName ()//function { if(string. IsNullOrEmpty (name)) {Name="My name"; } returnname; } } View Code
All of these constructors, events, fields, methods, and properties are called members, that is, member
Run the following code:
Type T =typeof(people); Console.WriteLine ("----------------Method------------------"); Methodinfo[] Methods=T.getmethods (); foreach(MethodInfo methodinchmethods) {Console.WriteLine ("Method:"+method); //Console.WriteLine (method); //Console.WriteLine ("Return Value:" + method.) Returnparameter); } Console.WriteLine ("---------------Field-------------------"); Fieldinfo[] Fields= T.getfields (BindingFlags.NonPublic | bindingflags.instance|bindingflags.static); foreach(FieldInfo FieldinchFields ) {Console.WriteLine ("Field:"+field); } Console.WriteLine ("--------------Member--------------------"); Memberinfo[] Members=t.getmembers (); foreach(MemberInfo memberinchMembers ) {Console.WriteLine ("Member:"+member); } Console.WriteLine ("--------------Property--------------------"); Propertyinfo[] Properties=t.getproperties (); foreach(PropertyInfo Propertyinchproperties) {Console.WriteLine ("Property :"+Property ); } Console.WriteLine ("--------------Constructor--------------------"); Constructorinfo[] Constructors= T.getconstructors (BindingFlags.NonPublic |bindingflags.instance); foreach(ConstructorInfo constructorinchconstructors) {Console.WriteLine ("Constructor:"+constructor); } View Code
The output is:
----------------Method------------------Method:System.String get_sex () method:void set_sex (System.String) Method:System.String get_name () Met Hod: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 () Mem Ber: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 Me Mber:System.String Name--------------Property--------------------Property:System.String Sex Property:System.String Name--------------Constructor--------------------constructor:void. ctor () Press any key to continue ...
View Code
In addition, we found that the property Sex and Name compiled into a get_sex,get_name,set_sex,set_name, hehe, it seems to be more Java-like
Original source: http://www.cnblogs.com/shuzhengyi/archive/2010/10/12/1848991.html
C # Reflection All fields, properties, methods (Go) in a class