The foundation is the top priority ~ Emit Dynamic Construction Method (parameters and return values)

Source: Internet
Author: User
Tags emit

The foundation is the top priority ~ Emit Dynamic Construction Method (parameters and return values)

Back to directory

For Emit, we know that it can dynamically build an assembly, type, method, attribute, and so on, or dynamically create Emit as long as you manually create something using C, due to its special characteristics, Emit has been widely used in many fields. For example, with the recent popular AOP technology, its core function is method interception, we can use Emit to implement the method blocking function. For details, refer to uncle's article Lind. DDD. aspects uses Plugins to dynamically intercept Methods ~ AOP in Lind.

Construct and call methods with parameters and no return values
[TestMethod] public void Bucket Method () {// obtain the current application domain AppDomain appDm = AppDomain. currentDomain; // Initialize an instance of AssemblyName an = new AssemblyName (); // set the Assembly name. name = "EmitLind"; // dynamically create an application Assembly AssemblyBuilder AB = appDm in the current application domain. defineDynamicAssembly (an, AssemblyBuilderAccess. run); // dynamically create a module in the Assembly ModuleBuilder mb = AB. defineDynamicModule ("EmitLind"); // dynamically create a class TypeBuilder tb = mb in the module. defineType ("HelloEmit", TypeAttributes. public | TypeAttributes. class); // dynamically create a method MethodBuilder mdb = tb for the Class. defineMethod ("HelloWord", MethodAttributes. public, null, new Type [] {typeof (string)}); // obtain the ILGenerator ilG = mdb for this method. getILGenerator (); ilG. emit (OpCodes. ldstr, "Hello: {0}"); // load the parameters of the Input Method to the stack ilG. emit (OpCodes. ldarg_1); // call the Console. writeLine method, which outputs the passed characters ilG. emit (OpCodes. call, typeof (Console ). getMethod ("WriteLine", new Type [] {typeof (string), typeof (string)}); ilG. emit (OpCodes. ret); // create the Type object Type tp = tb of the class. createType (); // instantiate a Class object ob = Activator. createInstance (tp); // obtain the method in the class, and call the method through Invoke .. methodInfo mdi = tp. getMethod ("HelloWord"); mdi. invoke (ob, new object [] {"Hello Lind "});}
Method Construction and calling with parameters and returned values
Public void bucket methodret () {// obtain the current application domain AppDomain appDm = AppDomain. currentDomain; // Initialize an instance of AssemblyName an = new AssemblyName (); // set the Assembly name. name = "EmitLind"; // dynamically create an application Assembly AssemblyBuilder AB = appDm in the current application domain. defineDynamicAssembly (an, AssemblyBuilderAccess. run); // dynamically create a module in the Assembly ModuleBuilder mb = AB. defineDynamicModule ("EmitLind"); // dynamically create a class TypeBuilder tb = mb in the module. defineType ("HelloEmit", TypeAttributes. public | TypeAttributes. class); // dynamically create a method MethodBuilder mdb = tb for the Class. defineMethod ("HelloWorldReturn", MethodAttributes. public, typeof (string), new Type [] {typeof (string), typeof (string)}); // obtain the ILGenerator ilG = mdb for this method. getILGenerator (); ilG. emit (OpCodes. ldstr, "Hello: {0}-{1}"); // load the parameters of the Input Method to the stack ilG. emit (OpCodes. ldarg_1); ilG. emit (OpCodes. ldarg_2); // call the Console. writeLine method, which outputs the passed characters ilG. emit (OpCodes. call, typeof (Console ). getMethod ("WriteLine", new Type [] {typeof (string), typeof (string), typeof (string)}); // ilG. emit (OpCodes. pop); // Add this. // The returned value is partially LocalBuilder local = ilG. declareLocal (typeof (string); ilG. emit (OpCodes. ldstr, "Return Value: {0}"); ilG. emit (OpCodes. ldarg_1); ilG. emit (OpCodes. call, typeof (string ). getMethod ("Format", new Type [] {typeof (string), typeof (string)}); ilG. emit (OpCodes. stloc_0, local); ilG. emit (OpCodes. ldloc_0, local); ilG. emit (OpCodes. ret); // create the Type object Type tp = tb of the class. createType (); // instantiate a Class object ob = Activator. createInstance (tp); // obtain the method in the class, and call the method through Invoke .. methodInfo mdi = tp. getMethod ("HelloWorldReturn"); mdi. invoke (ob, new object [] {"Hello Lind", "OK "});}

Back to directory

Related Article

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.