Before we talk about the principle of reflection, we must first clarify the following concepts:
What is IL?
Il,intermediage Language, the intermediate language, is not the local machine language that the CPU can directly execute, and a two compilation process called "Just in Time" before it can be converted to a computer-recognizable instruction. In the. NET framework, any language, such as c++,vb.net,c#, will be converted to an intermediate language after it has been compiled by its own compiler. Il also has 2 other names: Cil,common intermediate Language;msil, Microsoft intermediate Language.
To view the Il Code, refer to this: http://www.cnblogs.com/darrenji/p/3967669.html
What is the CLR?
CLR, Common Language Runtime, Common language runtime. Il code needs to be compiled on-the-fly by the CLR to convert to machine code. The general process is:
Programming language Code compilation generates an assembly. The Pe/coff header in the assembly contains information to be viewed and used by the Windows operating system; The CLR header tells the operating system that this is a. NET assembly, and the assembly manifest describes the information of the Assembly itself, such as the assembly identity, the resources that the assembly contains, the files that make up the assembly, and so on ; metadata describes what the assembly contains, such as the modules, types, type members, the visibility of type members, and so on.
The CLR begins to work: Manage application domains, load and run assemblies, security checks, JIT-compile IL code into machine code, exception handling , garbage collection, and so on.
What is JIT?
JIT, just in time, compile instantly. In the CLR, the work of converting IL to machine code is given to the JIT compiler. Instant compilation occurs only when the method is first called.
In learning. NET is inevitably exposed to these three concepts, what are these three things and what is the relationship between them? We may be too much in the process of learning to focus on the CLR because the CLR is at the heart of the. NET framework, but I want to say that CTS and CLS are more important because they are the core of the CLR. In any programming language, if you want to execute on the. NET CLR, you must provide a compiler that compiles the program for that language into the metadata and IL that the. NET CLR knows, and conforms to the CTS rules. Not all languages are as consistent with the CTS specification as C #, after all, many languages appear earlier, and CTS appears behind, so there are some older languages that fail to conform to the CTS rules. This kind of language is in. NET has the following three ways to conform to the CTS specification:
-Change the language itself to comply with the CTS rules. For example, Visual Basic 6 has made a significant expansion and change, plus inherited features, which makes the new version of Visual Basic. NET almost a whole new language, as if C had been derived from the same year.
-The extension language itself is close to the CTS, but retains the syntax incompatible with the CTS, so that the CTS-compliant process is compiled in the same way that it does not conform to the CTS rules and is compiled into native code in the traditional manner. This is true for example, C + + with Managed Extension (abbreviated as mc++).
-The language itself is as constant as possible, and everything is achieved through a strong compiler design that is compatible with the CTS, Eiffel. For example, the CTS does not support multiple inheritance (multiple inheritance), but Eiffel supports multiple inheritance, and the Eiffel compiler can use interface and attribute to achieve multiple inheritance (this is quite ingenious, Interested readers may wish to look into it.)
OK, now let's look at the relationship between the three of them.
1) CTS Universal type System (Common type systems)
The CTS not only implements the variable compatibility type of COM, but also defines the type extension by means of user-defined types. Any to. NET platform as the target language must establish a mapping between its data type and the type of the CTS. All. NET language to share this type of system, to achieve seamless interoperability between them. The scheme also provides the inheritance between languages. For example, a user can derive a class written by C # in vb.net. We can think of CTS as the Superset (union) of all. NET languages, and the various languages that conform to CTS are actually just subset (intersection) of the CTS. The programs written by these languages, if you want to have the best compatibility to invoke each other (invoke) or inheritance, these languages must obtain a common subset, there is a common adherence to the specification, this is the CLS.
2) CLS Common Language Specification (Common Language specification)
Obviously, the difference between programming languages is not just the type. For example, some languages support multiple inheritance, some languages support unsigned data types, and some languages support operator overloading. Users should be aware of this, therefore. NET limits the interoperability issues raised by these differences by defining common language Specifications (Cls:common Language specification). The CLS has developed a minimal feature that must be supported by a language that targets the. NET platform, as well as the complete characteristics required for interoperability between the language and other. NET languages. Recognizing that this is important, the characteristic issues discussed here are not just simple grammatical differences between languages. For example, the CLS does not care about what keywords a language implements for inheritance, but only about how the language supports inheritance. The CLS is a subset of the CTS. This means that a language feature may conform to the CTS standard, but is beyond the scope of the CLS. For example, C # supports unsigned numeric types, which can pass the CTS test, but the CLS only recognizes symbolic numeric types. Therefore, if a user uses a C # unsigned type in a component, it may not be designed with languages that do not use unsigned types, such as vb.net. NET components for interoperability.
3) CLR Common language Runtime (Common Language Runtime)
To put it simply, the CLR is the implementation of the CTS, which means that the CLR is the application's execution engine and a fully functional class library, which is implemented in strict accordance with the CTS specification. As a program execution engine, the CLR is responsible for securely loading and running user program code, including garbage collection and security checks on unused objects. Code that runs under CLR monitoring, called Managed code (managed).
Okay, now start talking about reflection, don't be frightened, the principle of reflection is very simple ,. NET has two important parts: IL (intermediate language code) and metadata (metadata). The code we write is not a lot of classes, the class has a lot of members, when compiling the code, the metadata table is based on the code to the class of all the information is recorded in it (in fact, it is a data structure, organize the information of the class).
The reflection process, in contrast, is to find members of the class through the details of the class that are recorded in the metadata and to "revive" it (since the information recorded in the metadata is sufficiently detailed to find the IL Code of the class and use it according to the information recorded in the metadata).
In the final comparison:
meta-data formation: According to the specific content of the code to form a class of record information;
Reflection: Find the required code based on the records of the metadata;
As for instances, it is convenient to use the type class:
Type t = typeof (System.String);
memberinfo[] mis = t.getmembers ();
foreach (MemberInfo mi in mis)
{
Console.WriteLine (mi. MemberType + mi. Name);
}
you will get all the information about the string class;
What is reflection? The principle of reflection