Emit's ILGenerator

Source: Internet
Author: User
Tags emit mscorlib

In the previous blog ( say emit ), I described the basic technology implementation requirements, the difficulty and the scope of the target is very small, build a basic shelf. The only drawback to implementing the creation of assemblies, modules, types, and methods in code is the method body.

The method body is the internal logic of the method, and we need to describe it in IL code and inject it inside the method body. This naturally leads to two themes, IL code, and the tools used to inject IL code into the method body (ILGenerator). This blog will focus on these two themes. But this blog will not be able to speak the Il very detailed, only around the application of ILGenerator to explain. If you want to know the full picture of IL, I'd like to see the ECMA document (http://www.ecma-international.org/publications/standards/Ecma-335.htm).

Introduction to 2.1 CIL instructions

Here we have a preliminary understanding of IL directives through a few simple examples.

Create a new console project named "HelloWorld", Code like listing 2-1 (although I used HelloWorld to explain IL in my previous article, although not many blogs have used this example, I still bother to use it).

Code Listings 2-1 HelloWorld

Using System;

Namespace HelloWorld

{

Class Program

{

static void Main (string[] args)

{

Console.WriteLine ("Hello World");

}

}

}

Compile the above code and then use ILDASM to open the HelloWorld.exe and export the. Il file, which reads as follows:

Microsoft (R). NET Framework IL disassembler. Version 4.0.30319.1

Copyright (c) Microsoft Corporation. All rights reserved.

Metadata version:v4.0.30319

. assembly extern mscorlib

{

. PublicKeyToken = (B7 7A 5C E0)//. Z\v.4 ...

. ver 4:0:0:0

}

. Assembly HelloWorld

{

Slightly

}

. module HelloWorld.exe

MVID: {Cbb65270-d266-4b29-bac1-4f255546cda6}

. imagebase 0x00400000

. file Alignment 0x00000200

. Stackreserve 0x00100000

. subsystem 0x0003//Windows_cui

. corflags 0x00020003//Ilonly 32BITREQUIRED

Image base:0x049f0000

=============== CLASS Members Declaration ===================

. class private Auto ANSI BeforeFieldInit Helloworld.program

extends [mscorlib]system.object

{

. method private Hidebysig static void Main (string[] args) cil managed

{

. entrypoint

Code size (0xd)

. maxstack 8

Il_0000:nop

Il_0001:ldstr "Hello World"

Il_0006:call void [mscorlib] System.console::writeline (String)

Il_000b:nop

Il_000c:ret

}//End of method Program::main

. method public Hidebysig specialname rtspecialname

instance void. ctor () CIL managed

{

Code size 7 (0x7)

. maxstack 8

il_0000:ldarg.0

Il_0001:call instance void [mscorlib] System.object::.ctor ()

Il_0006:ret

}//End of method Program::.ctor

}//End of Class Helloworld.program

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.