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)