. Net reflection access private variables and private methods,. net Variables
The following is the practice code:
1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. linq; 5 using System. reflection; 6 using System. text; 7 using System. threading. tasks; 8 9 namespace ConsoleTest10 {11 class Program12 {13 static void Main (string [] args) 14 {15 // reflection read class private attributes 16 Person per = new Person ("ismallboy", "20102100104"); 17 Type t = per. getType (); 18 // obtain the private method 19 MethodInfo method = t. getMethod ("GetStuInfo", BindingFlags. nonPublic | BindingFlags. instance); 20 // access the private method without parameters 21 string strTest = method. invoke (per, null ). toString (); 22 // access the private method with the parameter 23 MethodInfo method2 = t. getMethod ("GetValue", BindingFlags. nonPublic | BindingFlags. instance); 24 object [] par = new object [2]; 25 par [0] = "ismallboy"; 26 par [1] = 2; 27 string strTest2 = method2.Invoke (per, par ). toString (); 28 29 // obtain the private field 30 PropertyInfo field = t. getProperty ("Name", BindingFlags. nonPublic | BindingFlags. instance); 31 // access private field value 32 string value = field. getValue (per ). toString (); 33 // set the private field value 34 field. setValue (per, "new Name"); 35 value = field. getValue (per ). toString (); 36} 37} 38 39 // <summary> 40 // personal information 41 // </summary> 42 class Person43 {44 private string Name {get; set ;} 45 private string StuNo {get; set;} 46 47 public Person (string name, string stuNo) 48 {49 this. name = name; 50 this. stuNo = stuNo; 51} 52 53 private string GetStuInfo () 54 {55 return this. name; 56} 57 58 private string GetValue (string str, int n) 59 {60 return str + n. toString (); 61} 62} 63}
If dynamic is used, it can also be as follows:
1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. linq; 5 using System. reflection; 6 using System. text; 7 using System. threading. tasks; 8 9 namespace ConsoleTest10 {11 class Program12 {13 static void Main (string [] args) 14 {15 // reflection read class private attributes 16 dynamic per = new Person ("ismallboy", "20102100104"); 17 Type t = per. getType (); 18 // obtain the private method 19 MethodInfo method = t. getMethod ("GetStuInfo", BindingFlags. nonPublic | BindingFlags. instance); 20 // access the private method without parameters 21 string strTest = method. invoke (per, null); 22 // access the private method with the parameter 23 MethodInfo method2 = t. getMethod ("GetValue", BindingFlags. nonPublic | BindingFlags. instance); 24 object [] par = new object [2]; 25 par [0] = "ismallboy"; 26 par [1] = 2; 27 string strTest2 = method2.Invoke (per, par); 28 29 // get private field 30 PropertyInfo field = t. getProperty ("Name", BindingFlags. nonPublic | BindingFlags. instance); 31 // access private field value 32 string value = field. getValue (per); 33 // set private field value 34 field. setValue (per, "new Name"); 35 value = field. getValue (per ); 36} 37} 38 39 // <summary> 40 // personal information 41 // </summary> 42 class Person43 {44 private string Name {get; set ;} 45 private string StuNo {get; set;} 46 47 public Person (string name, string stuNo) 48 {49 this. name = name; 50 this. stuNo = stuNo; 51} 52 53 private string GetStuInfo () 54 {55 return this. name; 56} 57 58 private string GetValue (string str, int n) 59 {60 return str + n. toString (); 61} 62} 63}