Reflection mechanisms are described as follows in MSDN: Reflection provides encapsulated assembly, module, and type objects ({
Track ('ctl00 _ MTCS_main_ctl00 | ctl00_MTCS_main_ctl01 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.type (VS.80). aspx "> Type ). You can use reflection to dynamically create instances of the type, bind the type to an existing object, or obtain the type from the existing object and call its method or access its fields and properties. If attributes are used in the code, you can use reflection to access them.
Its main role is:
You need to access the properties of the program metadata. (Metadata is defined as the data in a program that changes the behavior of a program rather than being processed .)
Check and instantiate the types in the Assembly.
Create a new type at runtime. Use {
Track ('ctl00 _ mtcs_main_ctl00 | ctl00_mtcs_main_ctl06 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.reflection.emit (vs.80). aspx "> class in system. reflection. emit.
Perform post-binding to access the type method created at runtime. See topic {
Track ('ctl00 _ mtcs_main_ctl00 | ctl00_mtcs_main_ctl07 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/k3a58006 (vs.80). aspx "> dynamic loading and use types.
Encapsulation is a common method in Object-Oriented Programming. For reflection purposes, dynamic creation of types and binding to existing objects are the most important applications.
The following describes how to use a specific instance (refer to http://www.cnblogs.com/fineboy/archive/2006/10/10/525348.htmlblog)
(If the DLL file name is TestReflect. dll)
First, create a dynamic link library.
1 using System;
2
3 namespace Webtest
4 {
5/** // <summary>
6 // ReflectTest abstract description.
7 /// </summary>
8 public class ReflectTest
9 {
10 public ReflectTest ()
11 {}
12
13 public string WriteString (string s)
14 {
15 return "Welcome," + s;
16}
17
18/*** // <summary>
19 // dsajkjflasjdfalksdjfaskfd
20 /// </summary>
21 /// <param name = "s"> </param>
22 /// <returns> </returns>
23 public static string WriteName (string s)
24 {
25 return "Welcome," + s;
26}
27
28 public string WriteNoPara ()
29 {
30 return "you are using a non-parameter method ";
31}
32}
33}
The following is a specific call method:
1 Public void test1 ()
2 {
3 system. reflection. Assembly ass;
4 type;
5 object OBJ;
6 try
7 {
8 ass = system. reflection. Assembly. LoadFile (@ "D:/testreflect. dll ");
9 type = ass. GetType ("webtest. reflecttest"); // The namespace and class name must be used.
10 system. reflection. methodinfo method = type. getmethod ("writestring"); // method name
11 OBJ = ass. createinstance ("webtest. reflecttest"); // The namespace and class name must be used.
12 string s = (string) method. Invoke (obj, new string [] {"jianglijun"}); // call an instance method
13
14 Response. Write (s + "<br> ");
15 method = type. GetMethod ("WriteName"); // method name
16 s = (string) method. Invoke (null, new string [] {"jianglijun"}); // call a static method
17 Response. Write (s + "<br> ");
18
19 method = type. GetMethod ("WriteNoPara"); // No parameter instance method
20 s = (string) method. Invoke (obj, null );
21 Response. Write (s + "<br> ");
22 method = null;
23}
24 catch (exception ex)
25 {
26 response. Write (Ex + "<br> ");
27}
28 finally
29 {
30 ass = NULL;
31 type = NULL;
32 OBJ = NULL;
33}
34}