. Net reflection (reflection) is very comprehensive and powerful, such as famous. net decompilation tool red gate's. net reflector is used. net reflection mechanism. Here is a simple instance (using the ConsoleProgram) To see how reflection is used in. net.
-
- Using system;
-
- Using system. reflection;
-
-
-
- Namespace mengliao. CSHARP. c13.s02
-
- {
-
- Class myclass
-
- {
-
- Private int count;
-
-
-
- Public myclass (INT value)
-
- {
- Count = value;
-
- }
-
-
-
- Public void M1 ()
-
- {
-
- Console. writeline ("called method 1 .");
-
- }
-
-
-
- Public static int m2 (int x)
-
- {
- Return x * X;
-
- }
-
-
-
- Public void m3 (int x, Double Y)
-
- {
-
- Console. writeline ("called method 3, paramaters: x = {0}, y = {1: e}.", x, y );
-
- }
-
-
-
- Public void M4 ()
-
- {
- Console. writeline ("called Method 4. Count = {0}", count );
-
- }
-
-
-
- Private Static string M5 (Double X) // Private Static method, which cannot be called directly, but can be bound to a delegate
-
- {
-
- Return math. SQRT (x). tostring ();
-
- }
-
- }
-
-
-
- Class Program
-
- {
- Public static void main ()
-
- {
-
- // Obtain the type object of myclass.CodeTo use the static method of type, you must specify the Assembly for the same purpose.
-
- // Type T = type. GetType ("mengliao. CSHARP. c13.s02. myclass, consoleapplication, version = 1.0.0.0, culture = neutral, publickeytoken = NULL ");
-
- Type T = typeof (myclass );
-
- // Instantiate myclass through activator for instance method call
- Object OBJ = activator. createinstance (T, new object [] {88 });
-
-
-
- Methodinfo [] Methods = T. getmethods (); // retrieve the list of all methods of myclass
-
-
-
- Foreach (methodinfo nextmethod in methods) // enumerate all methods
-
- {
-
- Console. writeline (nextmethod. tostring (); // display method information
- If (nextmethod. Name = "M1") // method M1
-
- {
-
- Nextmethod. Invoke (OBJ, null); // call method M1 using the OBJ object, no Parameter
-
- }
-
- If (nextmethod. Name = "m2") // method m2
-
- {
- // Static method, call method m2 using NULL, create a parameter array, and input 10
-
- Console. writeline ("called static method 2, return {0}", nextmethod. Invoke (null, new object [] {10 }));
-
- }
-
- }
-
-
-
- Methodinfo m3info = T. getmethod ("m3"); // obtain method m3
- M3info. Invoke (OBJ, new object [] {123, 0.456}); // call method m3 and input the corresponding two parameters
-
-
-
- // Obtain method M4. Call the method using the OBJ object without parameters.
-
- T. invokemember ("M4", bindingflags. invokemethod, null, OBJ, null );
-
-
-
- // Create a generic delegate runme and bind the static private method M5 of myclass
-
- Delegate runme = delegate. createdelegate (typeof (func <double, string>), T, "M5 ");
- Console. writeline ("Call delegate with M5: SQRT (2) = {0}", (func <double, string>) runme) (2); // call this delegate
-
-
- Console. Readline ();
-
- }
-
- }
-
- }
The reflection mechanism can be used to call various methods, including private and static methods. The comments in the program are very detailed and I will not talk about them here.
This article from the "mengliao software Studio" blog, please be sure to keep this source http://mengliao.blog.51cto.com/876134/511804