Link: http://blog.csdn.net/ccaakkee/article/details/2425950,CSDN ccaakkee
Http://blog.csdn.net/jaydawson/article/details/5539438,CSDN jaydawson
Code:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using system. reflection; namespace consoleapplication12 {class testclass {private int A; private int AA; Public int B; Public int BB; protected int C; protected int cc; Public testclass () {A = 1; B = 2; C = 3;} public int A {get {return a ;}set {A = value ;}} private int AA {get {return AA;} set {AA = value ;}} public void setvalue (int x, int y, int Z) {A = x; B = y; C = z;} public void DIS () {console. writeline ("A:" + A + "B:" + B + "C:" + C) ;}} class program {static void main (string [] ARGs) {type = typeof (testclass); object OBJ = activator. createinstance (type); If (OBJ = NULL) console. writeline ("object is null"); else {console. writeline ("Class Name:" + obj. tostring (); console. writeline ("Class Name:" + type. name); console. writeline ("class Full name:" + type. fullname);} fieldinfo [] field = NULL; // obtain the public member variable name console. writeline ("Public member variable:"); field = type. getfields (bindingflags. instance | bindingflags. public); foreach (fieldinfo fi in field) console. writeline (Fi. name); // obtain the Non-Public member variable name console. writeline ("non-public member variable:"); field = type. getfields (bindingflags. instance | bindingflags. nonpublic); foreach (fieldinfo fi in field) console. writeline (Fi. name); // obtain the member variable type and value string item = "A"; fieldinfo fie = type. getfield (item, bindingflags. instance | bindingflags. nonpublic); console. writeline ("{0}'s type:" + fie. fieldtype, item); console. writeline ("{0}'s value:" + fie. getvalue (OBJ), item); testclass test_class = new testclass (); console. writeline ("{0}'s value:" + fie. getvalue (test_class), item); // obtain the class method console. writeline ("\ n class method name:"); methodinfo [] MIS = type. getmethods (); foreach (methodinfo MI in MIS) console. writeline ("function 'name:" + MI. name); // obtain the class property console. writeline ("\ n class attributes:"); propertyinfo [] Pis = type. getproperties (); foreach (propertyinfo PI in PIS) {console. writeline ("this type has the property name:" + pi. name); console. writeline ("attribute type" + pi. propertytype. name); console. writeline ("readable" + pi. canread); console. writeline ("writable" + pi. canwrite); console. writeline ("property value:" + pi. getvalue (OBJ, null ). tostring ();} // obtain a property of the class string pro_str = "A"; propertyinfo pro_aa = type. getproperty ("A"); console. writeline ("{0}:" + pro_aa.getvalue (OBJ, null ). tostring (), pro_str );}}}
View code: 1. Obtain the Class Name:
Activator. createinstance () method:
Description on msdn: Create an instance of the specified type.
There are two usage methods:
Activator.CreateInstance (Type)Activator.CreateInstance (Type, Object[])
The differences between the two methods are as follows: creating a constructor without parameters and creating a constructor with parameters.
Note that for internal types, we only need the following code to obtain the types:
Type.GetType("NameSpaceClassName");
The following code must be used for externally referenced dll:
Type.GetType("NameSpaceClassName, AssemblyName");
Assembly class:
Indicates an assembly. It is a reusable, version-free, and self-describing application constructor block in the common language runtime.
Description on msdn: http://msdn.microsoft.com/zh-cn/library/system.reflection.assembly (V = vs.110). aspx
2. Get the member variable type and value:
Bindingflags enumeration:
Link: http://msdn.microsoft.com/zh-cn/library/system.reflection.bindingflags (V = vs.110). aspx
3. Methods for obtaining classes:
Methodinfo:
Discover method attributes and provide access to method metadata.
Http://msdn.microsoft.com/zh-cn/library/system.reflection.methodinfo (V = vs.110). aspx
4. Get the attributes of a class:
Propertyinfo:
Property attributes are found and access to property metadata is provided.
Http://msdn.microsoft.com/zh-cn/library/system.reflection.propertyinfo (V = vs.110). aspx
C # Notes on Using Reflection to get the member variable names, methods, and attributes of a class