C # reflection

Source: Internet
Author: User

I recently read some knowledge about C # reflection, and it is estimated that the API can be used at most. For more information, it is difficult to estimate the current level. So in the following article, as a summary of a stage.
For the reflection summary, I want to expand from the following aspects: First, reflection assembly, module, class member and member information; then, dynamic call of the class member method; the third aspect is the dynamic generation of assembly, modules, classes, and class members. Now, let's start by reflecting all kinds of information.
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
In the program, what if we want to dynamically load an assembly? 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.
An example of testing to load a weak naming assembly is as follows:
(1) create a project for the console application and check "create SOLUTION ".
(2) create a new class library project in the solution, and write a class and a method at will.
(3) In the console project, add the following code directly in the Main method without adding a reference:
Assembly assembly = Assembly. Load ("MyAssembly ");
& Nbsp; if (assembly! = Null)
{
Console. WriteLine ("loaded successfully ");
}
If the program is executed, an exception is thrown, indicating that the Assembly cannot be found. Why? Because we use a weak named assembly, the Load method will not be searched in the Global Assembly Cache, and the Assembly is not found in the application directory, so the program cannot be found. At this time, we need to modify the program slightly without adding code. We only need to add a reference to MyAssembly and re-run the program. The load is successful.
Next, let's take a look at how Load loads a strongly-named Assembly. This step is a little complicated. Find the directory where the MyAssembly. dll Assembly is located, usually in the bin \ Debug directory.
(1) generate a key pair File
Sn-k MyAssemblyKey. keys
You can also create a key pair file name by yourself.
(2) generate a public key file
Sn-p MyAssemblyKey. keys MyAssemblyPublicKey. PublicKey
Note: view the public key command: sn-tp MyAssemblyPublicKey. PublicKey

(3) create a strongly-named assembly.
It's easy to add the following features to the code that declares the namespace:
[Assembly: AssemblyKeyFileAttribute (@ "D: \ Test \ MyAssemblyKey. keys")]

(4) compile the project
(5) Add the assembly to the global cache of the Assembly
Gacutil-I MyAssembly. dll
At this time, go to the loaded assembly project and change the parameter in the Load method to "assembly name, Version = Version, culture = region information, PublicKeyToken = Public Key", then remove the reference to the Assembly and we will find that the program runs successfully. Indicates that the Assembly is found in the global cache.
Use the Load method to Load the Assembly, especially the strongly-named assembly, and apply security and deployment policies on the Assembly. We recommend that you use this method to dynamically Load the Assembly. For LoadFrom and LoadWithPartialName, we will give a brief introduction next time, and hope some soft friends can point out the mistakes or omissions in the text. See you later.

Article Source: http://www.diybl.com/course/1_web/webjs/200877/132108_2.html

Article Source: http://www.diybl.com/course/1_web/webjs/200877/132108.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.