The foundation is the heavy ~emit dynamic construction method (parameters and return values)

Source: Internet
Author: User
Tags emit

Back to Catalog

For emit we know that it can dynamically build assemblies, types, methods, properties, etc., or you can create them dynamically using emit, as long as they are created using C #, emit because of its special place, so it has been widely used in many fields, like the recent comparison of Fire AOP technology, Its most core function is the method interception, we use emit also can implement method interception function, detailed can see uncle this article "Lind.DDD.Aspects through plugins realization method dynamic interception ~lind AOP".

Methods that have parameters and no return values are constructed and called
[TestMethod] Public voidBulidmethod () {//get the current application domainAppDomain APPDM =Appdomain.currentdomain; //Initializes an instance of the AssemblyNameAssemblyName an =NewAssemblyName (); //set the name of the AssemblyAn. Name ="Emitlind"; //dynamically create an application set in the current application domainAssemblyBuilder AB =appdm.definedynamicassembly (AN, assemblybuilderaccess.run); //dynamically creating a module within an assemblyModuleBuilder MB = ab. DefineDynamicModule ("Emitlind"); //dynamic creation of a class within a moduleTypeBuilder TB = MB. DefineType ("Helloemit", Typeattributes.public |Typeattributes.class); //dynamically create a method for a classMethodBuilder mdb = tb. DefineMethod ("Helloword", Methodattributes.public,NULL,NewType[] {typeof(string) }); //get the ILGenerator of this methodILGenerator IlG =mdb.            GetILGenerator (); Ilg.emit (Opcodes.ldstr,"hello:{0}"); //loading parameters of incoming methods into the stackIlg.emit (opcodes.ldarg_1); //Call the Console.WriteLine method to output the incoming characterIlg.emit (Opcodes.call,typeof(Console). GetMethod ("WriteLine",NewType[] {typeof(string),typeof(string) }));            Ilg.emit (Opcodes.ret); //to create a type object for a classType TP =TB.            CreateType (); //instantiate a class            ObjectOB =activator.createinstance (TP); //gets the method in the class that invokes the call to trigger the method:MethodInfo MDI = TP. GetMethod ("Helloword"); Mdi. Invoke (ob,New Object[] {"Hello Lind" }); }
Methods that have parameters and return values are constructed and called
         Public voidBulidmethodret () {//get the current application domainAppDomain APPDM =Appdomain.currentdomain; //Initializes an instance of the AssemblyNameAssemblyName an =NewAssemblyName (); //set the name of the AssemblyAn. Name ="Emitlind"; //dynamically create an application set in the current application domainAssemblyBuilder AB =appdm.definedynamicassembly (AN, assemblybuilderaccess.run); //dynamically creating a module within an assemblyModuleBuilder MB = ab. DefineDynamicModule ("Emitlind"); //dynamic creation of a class within a moduleTypeBuilder TB = MB. DefineType ("Helloemit", Typeattributes.public |Typeattributes.class); //dynamically create a method for a classMethodBuilder mdb =TB. DefineMethod ("Helloworldreturn", Methodattributes.public,typeof(string),                NewType[] {typeof(string),typeof(string) }); //get the ILGenerator of this methodILGenerator IlG =mdb.            GetILGenerator (); Ilg.emit (Opcodes.ldstr,"hello: {0}-{1}"); //loading parameters of incoming methods into the stackIlg.emit (opcodes.ldarg_1);            Ilg.emit (opcodes.ldarg_2); //Call the Console.WriteLine method to output the incoming characterIlg.emit (Opcodes.call,typeof(Console). GetMethod ("WriteLine",NewType[] {typeof(string),typeof(string) ,typeof(string)})); //Ilg.emit (opcodes.pop);//Plus, that's a problem.//return Value SectionLocalBuilder 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",NewType[] {typeof(string),typeof(string) }));            Ilg.emit (opcodes.stloc_0, local);            Ilg.emit (opcodes.ldloc_0, local);            Ilg.emit (Opcodes.ret); //to create a type object for a classType TP =TB.            CreateType (); //instantiate a class            ObjectOB =activator.createinstance (TP); //gets the method in the class that invokes the call to trigger the method:MethodInfo MDI = TP. GetMethod ("Helloworldreturn"); Mdi. Invoke (ob,New Object[] {"Hello Lind","OK" }); }

Back to Catalog

The foundation is the heavy ~emit dynamic construction method (parameters and return values)

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.