Simple implementation method using DynamicMethod near IL

Source: Internet
Author: User
I have been learning Emit recently and have some knowledge about commands. I have summarized some small experiences in the frequent use of commands in IL.
It is to push the variables, parameters to the stack, and then call some methods. This will be done back and forth. The following is a simple implementation method code using DynamicMethod :)

Using System;
Using System. Collections. Generic;
Using System. Reflection;
Using System. Reflection. Emit;
Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{


DynamicMethod dm = new DynamicMethod ("Test", null,
New Type [] {typeof (string)}, typeof (string). Module );
ILGenerator il = dm. GetILGenerator ();
Il. Emit (OpCodes. Ldarg_0); // push the parameter to the stack.
MethodInfo call = typeof (Console). GetMethod ("WriteLine", new Type [] {typeof (string )});
Il. Emit (OpCodes. Call, call); // execute the Console. WriteLine method.
Il. Emit (OpCodes. Ret); // return the end
Action <string> test = (Action <string>) dm. CreateDelegate (typeof (Action <string> ));
Test ("henry ");

// The following Test1 method and Test method are the same, but IL seems to be somewhat different.
// Mainly reflect the variable settings, and the variable location will also affect the instruction
Dm = new DynamicMethod ("Test1", null,
New Type [] {typeof (string)}, typeof (string). Module );
Il = dm. GetILGenerator ();
Il. DeclareLocal (typeof (string ));
Il. Emit (OpCodes. Ldarg_0); // push the parameter to the stack.
Il. Emit (OpCodes. Stloc_0); // Save the value to the variable with the index 0.
Il. Emit (OpCodes. Ldloc_0); // push the variable with index 0 to the stack.
Call = typeof (Console). GetMethod ("WriteLine", new Type [] {typeof (string )});
Il. Emit (OpCodes. Call, call); // execute the Console. WriteLine method.
Il. Emit (OpCodes. Ret );
Test = (Action <string>) dm. CreateDelegate (typeof (Action <string> ));
Test ("henry ");

// You can push the following method by yourself, which is actually very simple.
// If it does not seem clear, copy the file to vs.net and view the instruction description :)
Dm = new DynamicMethod ("Test2", null,
New Type [] {typeof (string)}, typeof (string). Module );
Il = dm. GetILGenerator ();
Il. DeclareLocal (typeof (string ));
Il. Emit (OpCodes. Ldstr, "hello ");
Il. Emit (OpCodes. Ldarg_0 );
Call = typeof (string). GetMethod ("Concat", new Type [] {typeof (string), typeof (string )});
Il. Emit (OpCodes. Call, call );
Il. Emit (OpCodes. Stloc_0 );
Il. Emit (OpCodes. Ldloc_0 );
Call = typeof (Console). GetMethod ("WriteLine", new Type [] {typeof (string )});
Il. Emit (OpCodes. Call, call );
Il. Emit (OpCodes. Ret );
Test = (Action <string>) dm. CreateDelegate (typeof (Action <string> ));
Test ("henry ");
Console. Read ();



}

}
}

When you are familiar with some commands, things become simple and not complicated as you think.

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.