Using DynamicILInfo class Dynamic generation method

Source: Internet
Author: User

  ILGenerator is different from the DynamicILInfo class. it uses a byte array directly to set the IL code, exception, and local signature. Well, how can we get these byte arrays from ILGenerator to the DynamicILInfo class is different. it uses a byte array directly to set the IL code, exception, and local signature. So, how can we get these byte arrays, see the following code:

usingSystem;usingSystem.Reflection;usingSystem.Reflection.Emit;classdemo{Static voidMain () {dynamicmethod DM=NewDynamicMethod ("HelloWorld",typeof(void), Type.emptytypes,typeof(Demo),false); DynamicILInfo il=DM.        Getdynamicilinfo (); SignatureHelper Sighelper=Signaturehelper.getlocalvarsighelper (); Il.        Setlocalsignature (Sighelper.getsignature ()); byte[] Code = {0x00,0x72,0x01,0x00,0x00,0x70,0x28,0x04,0x00,0x00,0x0a,0x00,0x2a }; inttoken0 = il. Gettokenfor ("Hello World"); inttoken1 = il. Gettokenfor (typeof(Console). GetMethod ("WriteLine",NewType[] {typeof(string) }).        Methodhandle); PutInteger4 (Token0,0x0002, code); PutInteger4 (Token1,0x0007, code); Il. Setcode (Code,8); Dm. Invoke (NULL,NULL);
Console.read (); } Static voidPutInteger4 (intValueintStartpos,byte[] Array) {Array[startpos++] = (byte) value; Array[startpos++] = (byte) (Value >>8); Array[startpos++] = (byte) (Value >> -); Array[startpos++] = (byte) (Value >> -); }}

The following is an explanation of Microsoft Enginee:

If The dynamic method we are going to create was based on a static method in some existing assembly, we can view the IL cod E (bytes) of the static method via ILDasm or by using Methodbody.getilbytearray. We can then build the byte array for setcode from there. For all tokens used in the original static method, being sure to replace them (bold, as shown above and below in this example ) with those generated from gettokenfor.

. method public hidebysig static void HelloWorld () cil managed
sig:00 00 01
{
Method begins at RVA 0x2050
Code size (0xd)
. maxstack 8
il_0000:/* 00 | */NOP
IL_0001:/* 72 | (000001)*/ldstr "Hello World"
IL_0006:/* 28 | (0A) 000004*/Call void [Mscorlib]system.console::writeline (String)
IL_000B:/* 00 | */NOP
IL_000C:/* 2A | */RET
}

To build the byte array for setexception, I ' d suggest taking a couple of minutes to read ECMA spec Partiton II (25.4.6) fi Rst. The flags/tryoffset/trylength/handleroffset/handlerlength ... can retrieved from Methodbody.exceptionhandlingclauses programatically. Be aware the exception type token (Classtoken) have to is obtained from gettokenfor as well. Do you know Ildasm can show the raw exception information? I did not know the until I learned about it recently from Peli.:) Uncheck View->expand try/catch and check show bytes; We'll see something like the following at the bottom of the method IL:

Exception Count 1
. Try il_002e to il_0051 finally handler il_0051 to il_0065
Hex:02 2E 00 00 00 23 00 00 00 51 00 00 00 14 00 00 00 00 00 00 00
}

Class SignatureHelper offers a convenient the construct the local variable signature, if each local variable type is kn Own. We can just make calls of signaturehelper.addargument.

Is there any scenarios in your mind where using dynamicilinfo could is better than using Dynamicilgenerator? How ' d you like to construct those byte arrays? I am thinking about writing and sharing a tool, which, based in an existing method, generates C # code to define the Equiva Lent dynamic method (using DynamicILInfo) (if this turns out to be a popular scenario).

Using DynamicILInfo class Dynamic generation method

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.