What is Emit, what is reflection, and what is the difference between the two?

Source: Internet
Author: User
Let's take a look at Microsoft's answer to the accurate definition of Emit.System. Reflection. Emit namespace contains {

AllowCompiler or toolSend metadata andIssueMicrosoft intermediate language (MSIL), and you can choose to generate a PE File class on the disk.

The main clients of these classes are the script engine and compiler.

}

 

 

Emit allows the compiler to issue metadata and msil directly. What are the advantages of Emit?MSIL is an intermediate process of converting. NET code into a machine language. It is a pseudo-assembly language between an advanced language and an Intel-based assembly language. When a user compiles a. NET Program, the compiler translates the source code into a Microsoft intermediate language (MSIL), which is a set of commands that can be effectively converted into local code and are independent of the CPU. When these commands are executed, the real-time (JIT) compiler converts them into CPU-specific code.

 

So, our. NET programs are eventually stored in these intermediate languages. After the program is run, the real-time compiler directly converts the program into a real machine language for execution. the advantage of emit is that it can directly generate IL running (flexible and efficient) in the process of dynamic program running)

 

 

 

Reflection principle and performance problems

Reference Jeffrey Richter:Metadata is stored in a series of tables. when an assembly or module is generated, the compiler creates a type definition table, a field definition table, a method definition table, and other tables. use the reflected namespace (sytem. reflection, so we can know that emit is an affiliated, not independent, and contains some types. You can use code to parse (called reflection) These metadata tables.

Reflection has two disadvantages: 1. reflection cannot ensure type security during compilation; 2. slow reflection speed. (We need to use sytem. the metadata of the Type Scanning Assembly in the reflection namespace, reflection must continuously execute string SEARCH)

 

 

 

Summary

Reflection includes emit. The cause of low reflection performance is that the metadata table of the assembly or module needs to be scanned. emit has high performance and is capable of dynamically generating msil for immediate execution. they have their own location, so when you use emit, you actually use reflection (at least you need it ). to improve performance, we can dynamically create some methods to achieve what we want (Dynamic proxy ).

(I hope to correct the terminology)

 

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.