. Net reflection access private variables and private methods,. net Variables
The following is the practice code:
Using System; using System. collections. generic; using System. componentModel; using System. linq; using System. reflection; using System. text; using System. threading. tasks; namespace ConsoleTest {class Program {static void Main (string [] args) {// reflection read class private attribute Person per = new Person ("ismallboy", "20102100104 "); type t = per. getType (); // obtain the private method MethodInfo method = t. getMethod ("GetStuInfo", BindingFlags. nonPublic | BindingFlags. instance); // access the non-parameter private method string strTest = method. invoke (per, null ). toString (); // access the private method MethodInfo method2 = t. getMethod ("GetValue", BindingFlags. nonPublic | BindingFlags. instance); object [] par = new object [2]; par [0] = "ismallboy"; par [1] = 2; string strTest2 = method2.Invoke (per, par ). toString (); // obtain the private field PropertyInfo field = t. getProperty ("Name", BindingFlags. nonPublic | BindingFlags. instance); // access the private field value string value = field. getValue (per ). toString (); // set the private field value field. setValue (per, "new Name"); value = field. getValue (per ). toString () ;}/// <summary> // personal information /// </summary> class Person {private string Name {get; set ;} private string StuNo {get; set;} public Person (string name, string stuNo) {this. name = name; this. stuNo = stuNo;} private string GetStuInfo () {return this. name;} private string GetValue (string str, int n) {return str + n. toString ();}}}
If dynamic is used, it can also be as follows:
Using System; using System. collections. generic; using System. componentModel; using System. linq; using System. reflection; using System. text; using System. threading. tasks; namespace ConsoleTest {class Program {static void Main (string [] args) {// reflection read class private attribute dynamic per = new Person ("ismallboy", "20102100104 "); type t = per. getType (); // obtain the private method MethodInfo method = t. getMethod ("GetStuInfo", BindingFlags. nonPublic | BindingFlags. instance); // access the non-parameter private method string strTest = method. invoke (per, null); // access the private method MethodInfo method2 = t. getMethod ("GetValue", BindingFlags. nonPublic | BindingFlags. instance); object [] par = new object [2]; par [0] = "ismallboy"; par [1] = 2; string strTest2 = method2.Invoke (per, par ); // obtain the private field PropertyInfo field = t. getProperty ("Name", BindingFlags. nonPublic | BindingFlags. instance); // access the private field value string value = field. getValue (per); // set the private field value field. setValue (per, "new Name"); value = field. getValue (per) ;}/// <summary> // personal information /// </summary> class Person {private string Name {get; set ;} private string StuNo {get; set;} public Person (string name, string stuNo) {this. name = name; this. stuNo = stuNo;} private string GetStuInfo () {return this. name;} private string GetValue (string str, int n) {return str + n. toString ();}}}
Provided by "figure douluo"