Recently, I studied the design pattern. I used reflection when I learned about the abstractfactory of terrylee. By the way, I also studied the reflection mechanism of C. I collected some information on the Internet and my personal understanding for everyone to learn.
In C #, we need to use reflection. First, we need to understand the relationships between several classes in the following namespace:
System. Reflection namespace
(1) appdomain: application domain, which can be understood as a logical container of a group of assemblies
(2) Assembly: Assembly class
(3) module: module class
(4) type: Use reflection to obtain the core class of the type information.
An appdomain can contain N assemblies, an assembly can contain N modules, and a module can contain N types. the appdomain class will be explained later. Let's first focus on Assembly classes. What if we want to dynamically load an assembly in the program? There are several ways to use static methods of Assembly: load, loadfrom and loadwithpartialname.
Let's first explain assembly. load method. This method has multiple overloaded versions. One of them is to provide detailed information about the Assembly, that is, the Assembly ID, including the Assembly name, version, region information, and public key tag, all are provided in the form of a string, such as: "myassembly, version = 1.0.0.0, culture = ZH-CN, publickeytoken = 47887f89771bc57f ".
So what is the order in which the Assembly is loaded using assembly. load? First, it will go to the Global Assembly Cache for search, then to the application root directory, and finally to the application's private path for search.
Of course, if you are using a weak named Assembly, that is, only the Assembly name is given, then CLR will not apply any security or deployment policies on the Assembly at this time, in addition, load does not go to the global cache program to find the assembly.
The usage of Assembly. Load ("") is described as follows;
It is not a namespace. Commonly used is the Assembly name, that is, the DLL name.
Reload list
Description
Assembly. Load (assemblyname) loads an Assembly Based on the assemblyname of the given assembly.
Supported by. NET Compact framework.
Assembly. Load (byte []) loads an assembly with an image based on the Common Object File Format (coff). The image contains the issued assembly. Load the assembly to the caller's domain.
Assembly. Load (string) loads an assembly by the long format name of the given assembly.
Supported by. NET Compact framework.
Assembly. Load (assemblyname, eviname) loads an Assembly Based on the assemblyname of the given assembly. Load the assembly to the caller's domain using the provided evidence.
Assembly. Load (byte [], byte []) loads an assembly with an image based on the Common Object File Format (coff). The image contains the issued assembly.
Assembly. Load (string, evi) loads the Assembly by the display name of the given assembly, and loads the assembly to the caller's domain using the evidence provided.
Assembly. Load (byte [], byte [], evidence) loads an assembly with an image based on the Common Object File Format (coff). This image contains an Assembly that has been issued.
Reflection assembly. Load ("assembly"). createinstance ("namespace. Class ")
No matter which layer writes this code, the ("assembly") reads the DLL in the bin folder of the web layer, that is to say, the Assembly DLL of the class you reflected must have
Note that createinstance () must be the namespace. Class Name; otherwise, the created instance is empty.
Assembly. Load ("assembly name ")
Assembly. loadfrom ("actual assembly path ")