C # Reflection All fields, properties, methods (Go) in a class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.