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