In C #, we want to use reflection, first to understand the relationships of several classes in the following namespaces:
System.Reflection namespaces
(1) AppDomain: An application domain that can be understood as a logical container for a set of assemblies
(2) Assembly: assembly class
(3) Module: Modular class
(4) Type: The most core class for using reflection to get type information
There is a dependency between them, that is, an AppDomain can contain N assembly, a assembly can contain n module, and a module can contain n type. The AppDomain is a class we'll explain later. Let's focus on the Assembly class, in the program, what if we want to load an assembly dynamically? There are several ways to use it, namely the static method of Load,loadfrom and LoadWithPartialName three assembly.
To explain the Assembly.Load method, the method will have multiple overloaded versions, one of which is to provide the details of the Assembly, that is, the identity of the Assembly, including the assembly name, version, zone information, public key token, all provided as a string, for example: " myassembly,version=1.0.0.0,culture=zh-cn,publickeytoken=47887f89771bc57f ".
So what is the order of loading assemblies using Assembly.Load? First it goes to the global assembly cache lookup, then to the application's root directory, and finally to the application's private path lookup.
Of course, if you are using a weakly named assembly, which gives you only the name of the assembly, then at this point the CLR will not apply any security or deployment policies on the assembly, and load will not find the assembly in the global cache assembly.
Instructions for use of Assembly.Load ("") are as follows:
parameter is not a namespace, commonly used is the assembly name, which is the name of the DLL
Overloaded List Name Description:
Assembly.Load (AssemblyName) loads an assembly in the context of the assemblyname of the given assembly. Supported by. Netcompactframework.
Assembly.Load (byte[]) loads an assembly with an image based on a common object file format (COFF) that contains the emitted assembly. Loads the assembly into the caller's domain.
Assembly.Load (String) loads an assembly by the long format name of the given assembly. Supported by. Netcompactframework.
Assembly.Load (assemblyname,evidence) loads an assembly in the context of the assemblyname of the given assembly. Loads the assembly into the caller's domain using the provided evidence.
Assembly.Load (byte[],byte[]) loads an assembly with an image based on a common object file format (COFF) that contains the emitted assembly.
Assembly.Load (string,evidence) loads the assembly by the display name of the given assembly, using the provided evidence to load the assembly into the caller's domain.
Assembly.Load (byte[],byte[],evidence) loads an assembly with an image based on a common object file format (COFF) that contains the emitted assembly.
About Reflection Assembly.Load ("assembly"). CreateInstance ("namespace. Class")
No matter what level of writing this code ("assembly") reads the actual DLL under the Web Layer Bin folder, that is, the assembly DLL of the class you are reflecting must be in the bin of the web layer, note createinstance () Must be a namespace. Class name, otherwise the created instance is empty
Assembly.Load ("assembly name")
Assembly.LoadFrom ("Assembly Physical Path")
What is assembly (assembly)?
Assembly is a collection of information that contains the name, version number, self-description, file association, and file location of the program. Supported by the Assembly class in the. NET Framework, the class is located under System.Reflection, and the physical location is located at: mscorlib.dll.
What can assembly do?
We can use assembly information to obtain the program's class, instance, and other programming needs.
A simple Demo Example:
1. Establish a console project named: Namespaceref
2. Write the following code:
[CSharp] View plaincopy
- Using System;
- Using System.Collections.Generic;
- Using System.Text;
- Using System.Reflection;
- Namespace Namespaceref
- {
- Class Program
- {
- static void Main (string[] args)
- {
- Country CY;
- String assemblyname = @"Namespaceref";
- string strongclassname = @"Namespaceref.china";
- //NOTE: Here the class name must be a strong class name
- //AssemblyName can be found through the AssemblyInfo.cs of the project
- cy = ountry) Assembly.Load (AssemblyName). CreateInstance (Strongclassname);
- Console.WriteLine (Cy.name);
- Console.readkey ();
- }
- }
- Class Country
- {
- public string name;
- }
- class Chinese:country
- {
- Public Chinese ()
- {
- name = "Hello";
- }
- }
- class America:country
- {
- Public America ()
- {
- Name = "Hello";
- }
- }
- class China:country
- {
- Public China ()
- {
- Name = "This is the class to invoke";
- }
- }
- }
Because of the existence of assembly, we have a better choice in implementing the design pattern. We sometimes encounter such a problem when we develop, to create the specified object according to the corresponding name. Such as: Give Chinese to create a Chinese object, previously we can only write code: if (strongclassname = = "China") {cy = new China (); } else if (Strongclassname = = "America") {cy = new America (); }
So if we have a long list of objects to create, this code is difficult to maintain and not easy to read. Now we can get an example by defining the assembly name of the class and the strong name of the class in the external file, which is easy to understand and enhances extensibility without modifying the code.
cy= (country) Assembly.Load (AssemblyName). CreateInstance (Strongclassname);
Create a file that has two parts in the solution: a declaration reference and a code entity.
A declaration reference is a using System that refers to all the members under the System namespace, referencing all of the system-derived classes.
There is also a file called AssemblyInifo.cs.
Assembly is part of building a. NET application, with version information and self-describing information, and can be reused.
A simple assembly can be a single executable file that contains publication and version information, but typically contains multiple executables and resource files.
Assembly defines security, release, and type resolution, which is the smallest unit that can be executed, and the running environment can only execute code that is located in assembly.
Assembly is important for the common language runtime Environment (Common LANGUAGERUNTIME,CLR) because the CLR uses him to locate and secure code execution.
Assemblytitle Title
Assemblydescription description
Assemblyconfiguration Configuration
Assemblycomany Corp.
Assemblyproduct Products
Assemblycopyright Copyright
Assemblytrademark Trademark
Assemblyculture Area
Assembly version information, including four segment values: Major version number, sub-version number, revision number, compile count.
Article reprinted from 10999777
(GO) C # assembly.load use