First, I must admit that it is not a good practice to access a private member of a class. We all know that Private Members cannot be accessed from outside. A class contains many private members, such as private fields, private attributes, and private methods. For private member access, you can use the following method to solve this problem.
Private stringName;Public StringName {Get{ReturnName ;}Set{Name =Value;}}
But sometimes,Source codeYou cannot modify the source because it is owned by others.Code, Only provide DLL to you. Or you can maintain others' code, but the source code is lost. In this case, if you want to know the value of the Private member or even directly call the private method in the class. What should we do? In fact, it is not difficult to access private members in. net.ArticleProvide a few simple methods for you to get what you want.
To make the code more elegant, use the extension method.
1. Obtain the private field value:
Public staticT getprivatefield <t> (This objectInstance,StringFieldname ){BindingflagsFlag =Bindingflags. Instance |Bindingflags. Nonpublic;TypeType = instance. GetType ();FieldinfoField = type. getfield (fieldname, flag );Return(T) field. getvalue (instance );}
2. Obtain the private property value:
Public staticT getprivateproperty <t> (This objectInstance,StringPropertyname ){BindingflagsFlag =Bindingflags. Instance |Bindingflags. Nonpublic;TypeType = instance. GetType ();PropertyinfoField = type. getproperty (propertyname, flag );Return(T) field. getvalue (instance,Null);}
3. Set the private member value:
Public static void Setprivatefield ( This object Instance, String Fieldname,Object Value ){ Bindingflags Flag = Bindingflags . Instance | Bindingflags . Nonpublic; Type Type = instance. GetType (); Fieldinfo Field = type. getfield (fieldname, flag); field. setvalue (instance, value );} 4. Set the private attribute value: Public static void Setprivateproperty ( This object Instance, String Propertyname,Object Value ){ Bindingflags Flag = Bindingflags . Instance | Bindingflags . Nonpublic; Type Type = instance. GetType (); Propertyinfo Field = type. getproperty (propertyname, flag); field. setvalue (instance, value, Null );} 5. Call Private methods:
Public staticT callprivatemethod <t> (This objectInstance,StringName,Params object[] PARAM ){BindingflagsFlag =Bindingflags. Instance |Bindingflags. Nonpublic;TypeType = instance. GetType ();MethodinfoMethod = type. getmethod (name, flag );Return(T) method. Invoke (instance, Param );}
Test:
Next we will use a test class for testing. Create a class library project and test the Class Code as follows:
Public class Testclass { Public Testclass () {privatefield1 = 1; privatefield2 = 99; privatefielda = "Lo" ; Privatefieldb = "Ve" ;} Private int Privatefield1; Private int Privatefield2; Private string Privatefielda { Get ; Set ;} Private string Privatefieldb { Get ;Set ;} Private int Add (){ Return Privatefield1 + privatefield2 ;} Private string Join (){ Return Privatefielda + privatefieldb ;}}
Introduce the DLL of the above class library into the console project. Use the following code to use private members of this class:
Testclass OBJ = New Testclass (); System. Console . Writeline ( "Private field" ); System. Console . Writeline (obj. getprivatefield < Int > ( "Privatefield1" ); System. Console . Writeline (obj. getprivatefield < Int > ( "Privatefield2" ); System. Console . Writeline ( "Private Property" ); System. Console . Writeline (obj. getprivateproperty < String > ( "Privatefielda" ); System. Console . Writeline (obj. getprivateproperty < String > ( "Privatefieldb" ); System. Console . Writeline ( "Private method" ); System. Console . Writeline (obj. callprivatemethod < Int > ( "Add" , Null ); System. Console . Writeline (obj. callprivatemethod < String > ( "Join" ,Null ); System. Console . Writeline ( "Modifying private attributes" ); Obj. setprivateproperty ( "Privatefielda" , "Hello" ); Obj. setprivateproperty ( "Privatefieldb" , "World" ); System. Console . Writeline (obj. callprivatemethod < String > ( "Join" , Null ); System. Console . Read ();
The result is as follows:
Summary:Access to private members of the class.
From: http://www.cnblogs.com/zhuqil/archive/2010/07/25/Access-Private-Member.html and share with you.