1. Use reflection to dynamically create class instances and call their public member functions. // Create a class library project and add a getsum method. Using system; namespace classlibrary1 {public class class1 {public class1 () {}public int getsum (int x, int y) {return x + y ;}}} // create another project and reference the classlibrary1.dll system generated above in the project. reflection. assembly A = system. reflection. assembly. loadfrom ("classlibrary1.dll"); system. type T =. getType ("classlibrary1.class1"); // dynamically generate an instance of the classlibrary1.class Class Object theobj = system. activator. createinstance (t); // parameter information, G Etsum requires two int parameters: system. type [] paramtypes = new system. type [2]; paramtypes [0] = system. type. getType ("system. int32 "); paramtypes [1] = system. type. getType ("system. int32 "); system. reflection. methodinfo MI = T. getmethod ("getsum", paramtypes); // parameter value: object [] parameters = new object [2]; Parameters [0] = 3; Parameters [1] = 4; object returnvalue = mi. invoke (theobj, parameters); console. writeline ("classlibrary1.cia Ss1.getsum (3, 4) returns: {0} ", returnvalue. tostring (); 2. Reflection of Private Members of the response class. If it is C ++, we can calculate the position of the members in the object, and then offset the pointer to access all non-public members of the type. However, the. NET object is completely managed by GC, and the address cannot be obtained at all, and the method cannot be called through the pointer.
Of course... this is a very unrecommendable technique. Access to non-public members may damage the object state and cause unpredictable consequences. However, the reflection mechanism of. Net can be used in any case.
For example, a class:
Class myclass
{
Private string privatefield = "private field ";
Protected string protectedfield = "protected field ";
Private string _ protectedproperty = "protected property ";
Protected string protectedproperty
{
Get {return _ protectedproperty ;}
Set {_ protectedproperty = value ;}
}
Private string _ privateproperty = "private property ";
Private string privateproperty
{
Get {return _ privateproperty ;}
Set {_ privateproperty = value ;}
}
Protected void protectedmethod ()
{
Console. writeline ("protected method invoked ");
}
Private void privatemethod ()
{
Console. writeline ("Private method invoked ");
}
}
No member except the default constructor is public, but I still want to get and set the value of field and property, and call the two methods. The method is:
Myclass MC = new myclass ();
Type T = typeof (myclass );
Bindingflags BF = bindingflags. instance | bindingflags. nonpublic;
// Fields
Fieldinfo fi_protected = T. getfield ("protectedfield", BF );
Fieldinfo fi_private = T. getfield ("privatefield", BF );
Console. writeline (fi_protected.getvalue (MC ));
Console. writeline (fi_private.getvalue (MC ));
Fi_private.setvalue (MC, "new private field ");
Console. writeline (fi_private.getvalue (MC ));
Console. writeline ();
// Properties
Propertyinfo pi_protected = T. getproperty ("protectedproperty", BF );
Propertyinfo pi_private = T. getproperty ("privateproperty", BF );
Console. writeline (pi_protected.getvalue (MC, null ));
Console. writeline (pi_private.getvalue (MC, null ));
Pi_private.setvalue (MC, "new private property", null );
Console. writeline (pi_private.getvalue (MC, null ));
Console. writeline ();
// Methods
Methodinfo mi_protected = T. getmethod ("protectedmethod", BF );
Methodinfo mi_private = T. getmethod ("privatemethod", BF );
Mi_protected.invoke (MC, null );
Mi_private.invoke (MC, null );
Console. Readline ();
Output:
Protected field
Private Field
New private field
Protected property
Private property
New private property
Protected method invoked
Private method invoked
Event, the same operation can be performed, eventinfo