Emit study (1)-HelloWorld

Source: Internet
Author: User
Tags emit

I've seen Dapper before (using emit), Cyq. Data (another idea, no use of the emit) class framework, but also want to make a small frame to play, but at this time too much capacity, do not cyq.data or pdf.net such a framework, so began the road to learning. Start by making a small goal that you can achieve, starting with emit.

First, the use of the scene

Emit usage scenarios, usually we can choose to use emit in the following situations:

1. Dynamic creation of types, modules, etc. at the same time, but also need to improve efficiency (can be dynamically compiled once, and then no more processing).

2. Delay the use of bound objects.

3. Tool plug-ins and IDE development.

4. ORM implementation.

5. Reduce the performance loss of reflection.

Second, small example

The following sections are reproduced from: http://www.cnblogs.com/gjhjoy/p/3627243.html ( if infringement, please contact me to delete this part of the content )

classProgram {Static voidMain (string[] args) {            //1. Building Assemblies            varAsmname =NewAssemblyName ("Test"); varAsmbuilder =AppDomain.CurrentDomain.DefineDynamicAssembly (Asmname, Assemblybuilderaccess.runandsave); //2. Create a module            varMdlbldr = Asmbuilder.definedynamicmodule ("Elvinle","Elvinle.exe"); //3. Defining Classes            varTypebldr = Mdlbldr.definetype ("Hello", Typeattributes.public); //4. Define class members (methods, properties, and so on)//Public void SayHello () {}            varMethodbldr = Typebldr.definemethod ("SayHello", Methodattributes.public | Methodattributes.static,NULL,NULL); //5. Build the method body//Get Il generator            varIl =Methodbldr.getilgenerator (); Il. Emit (Opcodes.ldstr,"Hello, HelloWorld."); Il. Emit (Opcodes.call,typeof(Console). GetMethod ("WriteLine",NewType[] {typeof(string) })); Il. Emit (Opcodes.call,typeof(Console). GetMethod ("ReadLine")); Il. Emit (Opcodes.pop);//The read-in value is pushed to the evaluation stack, and this method has no return value, so the value on the stack needs to be discardedIL.            Emit (Opcodes.ret); //call CreateType to complete the creation of the type            vart =Typebldr.createtype (); //set entry point to SayHello (),--equivalent to main ()Asmbuilder.setentrypoint (T.getmethod ("SayHello")); Asmbuilder.save ("Elvinle.exe"); }    }
View Code

The following sections are reproduced from: http://www.cnblogs.com/Mervin/archive/2013/04/05/reflection-3.html (if infringement, please contact me to delete this part of the content)

Several classes commonly used with emit are as follows:

Class name Use
AssemblyBuilder Used to define and create dynamic assemblies
ConstructorBuilder Constructor used to create a dynamic class
CustomAttributeBuilder Used to create user-defined attributes
MethodBuilder A method used to create a dynamic class, or a constructor, because the constructor itself is a special method
ModuleBuilder To create a module in a dynamic assembly
TypeBuilder Used to define and create a new instance of a dynamic class
DynamicMethod Used to create dynamic methods that can be dynamically compiled and executed
ILGenerator Used to generate an intermediate language, the MSIL instruction
Opcodes Provides a field representation of Microsoft intermediate language (MSIL) directives

First look at the general steps for using emit:

1. Create an assembly.

2. Create a module within an assembly.

3. Create a dynamic class within the module.

4. Add dynamic methods, properties, events, and so on for dynamic classes.

5. Generate the associated IL code.

6. Return the created type or persist to the hard disk.

Third, the instruction details, a bit similar to the assembly

Since the previous computer vs installed in the English version, for some opcodes instructions, some do not understand very well, so attach a Chinese version of it

Address: Http://files.cnblogs.com/files/elvinle/OpCodes.rar

Emit study (1)-HelloWorld

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.