C # original code dynamic compilation function implementation ~

xiaoxiao2021-03-06  57

Original: http://ms.mblogger.cn/minBear/posts/2379.aspx

C # original code dynamic compilation function implementation ~

Mainly use System.Reflection.emit this AssemblyBuilder under this namespace, and Code is as follows:

[C #] using System; using System.Text; using System.Threading; using System.Reflection; using System.Reflection.Emit; // The Point class is the class we will reflect on and copy into our // dynamic assembly The. public static function PointMain () will be used // as our entry point.//// We are constructing the type seen here dynamically, and will write it // out into a .exe file for later execution from the command-line. // --- // Class Point {// // private int x; // private int y; //// public point (int in, int =) {//// THIS.X = ix; // THIS.Y = IY; ////} /// PUBLIC INT DOTPRODUCT (POINT P) {//// Return ((this.x * px) (THISY * PY)); //// } /// public static void pointmain () {/// Console.write ("Enter the 'x' value for point 1:"); // int x1 = convert.toint32 (console.readline ()); // // console.write ("Enter the 'y' Value for Point 1:"); // int y1 = convert.Toint32 (console.readline ()); //// console.write ("Enter the ' X 'Value for Point 2: "); // int x2 = convert.toint32 (console.readline ()); /// console.write (" Enter the 'y' Value for Point 2: "); // int y2 = conver. Toint32 (console.readline ()); /// / Point P1 = New Point (x1, y1); // Point P2 = new point (x2, y2); //// console.writeline ("({0} , {1}). ({2}, {3}) = {4}. ", // x1, y1, x2, y2, p1.dotproduct (p2)); ///} ////} // --- class AssemblyBuilderDemo {public static Type BuildDynAssembly () {Type pointType = null; AppDomain currentDom = Thread.GetDomain (); Console.Write ( "Please enter a name for your new assembly:");

StringBuilder asmFileNameBldr = new StringBuilder (); asmFileNameBldr.Append (Console.ReadLine ()); asmFileNameBldr.Append ( "exe."); String asmFileName = asmFileNameBldr.ToString (); AssemblyName myAsmName = new AssemblyName (); myAsmName.Name = "MyDynamicAssembly"; AssemblyBuilder myAsmBldr = currentDom.DefineDynamicAssembly (myAsmName, AssemblyBuilderAccess.RunAndSave); // We've created a dynamic assembly space - now, we need to create a module // within it to reflect the type Point into ModuleBuilder myModuleBldr. = myAsmBldr.DefineDynamicModule (asmFileName, asmFileName); TypeBuilder myTypeBldr = myModuleBldr.DefineType ( "Point"); FieldBuilder xField = myTypeBldr.DefineField ( "x", typeof (int), FieldAttributes.Private); FieldBuilder yField = myTypeBldr.DefineField ( "y", TYPEO f (int), FieldAttributes.Private);. // Build the constructor Type objType = Type.GetType ( "System.Object"); ConstructorInfo objCtor = objType.GetConstructor (new Type [0]); Type [] ctorParams = new type [] {typeof (int), typeof (int)}; ConstructorBuilder pointCtor = myTypeBldr.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard, ctorParams); ILGenerator ctorIL = pointCtor.GetILGenerator (); ctorIL.Emit (OpCodes.Ldarg_0) ;

Ctoril.emit (opcodes.call, objctor); ctoril.emit (opcodes.ldarg_0); ctoril.emit (opcodes.ldarg_1); ctoril.emit (opcodes.stfld, xfield); ctoril.emit (opcodes.ldarg_0); ctoril .Emit (OpCodes.Ldarg_2); ctorIL.Emit (OpCodes.Stfld, yField); ctorIL.Emit (OpCodes.Ret);. // Build the DotProduct method Console.WriteLine ( "Constructor built."); MethodBuilder pointDPBldr = myTypeBldr .DefineMethod ( "DotProduct", MethodAttributes.Public, typeof (int), new Type [] {myTypeBldr}); ILGenerator dpIL = pointDPBldr.GetILGenerator (); dpIL.Emit (OpCodes.Ldarg_0); dpIL.Emit (OpCodes.Ldfld , xfield; dpil.emit (opcodes.ldarg_1); dpil.emit (opcodes.ldfld, xfield); dpiL.emit (opcodes.mul_ovf_un); dpil.emit (opcodes.ldarg_0); dpil.emit (opcodes.ldfld, Yfield; dpil.emit (opcodes.ldarg_1); dpiL.emit (opcodes.ldfld, yfield ); DpIL.Emit (OpCodes.Mul_Ovf_Un); dpIL.Emit (OpCodes.Add_Ovf_Un); dpIL.Emit (OpCodes.Ret);. // Build the PointMain method Console.WriteLine ( "DotProduct built."); MethodBuilder pointMainBldr = myTypeBldr.DefineMethod ( "PointMain", MethodAttributes.Public | MethodAttributes.Static, typeof (void), null); pointMainBldr.InitLocals = true; ILGenerator pmIL = pointMainBldr.GetILGenerator ();

// We since Four Methods That We wish to call, and must represent as //MethodInfo tokens: // - void console.writeline () // - string console.readline () // - int convert.int32 (STRING) // - void console.writeline (string, object []) methodinfo writemi = typeof (console) .getMethod ("Write", new type [] {typeof (string)}); methodinfo readlinemi = typeof (console) .getMethod "Readline", new type [0]); methodinfo convertint32mi = typeof (communication) .getMethod ("toint32", new type [] {typeof (string)}); type [] wlparams = new type [] {typeof (String ), typeof (object [])}; MethodInfo writeLineMI = typeof (Console) .GetMethod ( "WriteLine", wlParams); // Although we could just refer to the local variables by // index (short ints for ldloc / stloc, BYTES for ldloc_s / stloc_s ), // this time, we'll use LocalBuilders for clarity and to // demonstrate their usage and syntax LocalBuilder x1LB = pmIL.DeclareLocal (typeof (int));. LocalBuilder y1LB = pmIL.DeclareLocal (typeof (int)); LocalBuilder x2LB = pmIL.DeclareLocal (typeof (int)); LocalBuilder y2LB = pmIL.DeclareLocal (typeof (int)); LocalBuilder point1LB = pmIL.DeclareLocal (myTypeBldr); LocalBuilder point2LB = pmIL.DeclareLocal (myTypeBldr); LocalBuilder tempObjArrLB = pmIL .Declareelocal (TypeOf (Object [])); PMIL.emit (opcodes.ldstr, "

ENTER THE 'X' Value for Point 1: "); Pmil.emitcall (Opcodes.call, Writemi, NULL); Pmil.emitcall (Opcodes.call, ReadLinemi, Null); Pmil.emitcall (Opcodes.call, Convertint32mi, NULL Pmil.emit (OpCodes.stloc, X1LB); PMIL.emit (opcodes.ldstr, "Enter the 'Y' Value for Point 1:"); PMIL.emitcall (Opcodes.call, Writemi, NULL); PMIL. Emitcall (OpCodes.call, ReadLinemi, NULL); Pmil.emitcall (opcodes.call, convertint32mi, null); Pmil.emit (Opcodes.stloc, Y1LB); Pmil.emit (opcodes.ldstr, "Enter the 'x' Value For Point 2: "); Pmil.emitcall (Opcodes.call, Writemi, NULL); Pmil.emitcall (Opcodes.call, ReadLinemi, Null); Pmil.emitcall (Opcodes.call, Convertint32mi, null); Pmil.emit OpCodes.stloc, X2LB); Pmil.emit (opcodes.ldstr, "Enter the 'Y' Value for Point 2:"); Pmil.emitcall (Opcodes.call, Writemi, Null); Pmil.emitcall (opcodes.call, Readlinemi, NULL; Pmil.emitcall (Opcodes.call, Convertint32mi, NULL); Pmil.emit (Opcodes.stloc, Y2LB); PM Il.emit (Opcodes.ldloc, X1LB); Pmil.emit (OpCodes.ldloc, Y1LB); Pmil.emit (OpCodes.Newobj, Pointctor); Pmil.emit (Opcodes.stloc, Point1LB); Pmil.emit (opcodes. LDLOC, X2LB); Pmil.emit (Opcodes.ldloc, Y2LB); Pmil.emit (opcodes.newobj, pointctor); Pmil.emit (OpCodes.stloc, Point2lb); Pmil.emit (Opcodes.ldstr, "({0 }, {1}) = {4}. "); PMIL.Emit (opcodes.ldc_i4_5); Pmil.emit (Opcodes.Newarr, Typeof (Object)); PMIL.emit (OpCodes.stloc, Tempobjarrlb); Pmil.emit (Opcodes.ldloc, Tempobjarrlb); Pmil.emit (Opcodes.ldc_i4_0); Pmil.emit (Opcodes.ldloc, X1LB); Pmil.Emit (Opcodes.box, Typeof )); PMIL.Emit (opcodes.stelem_ref);

Pmil.emit (Opcodes.ldloc, Tempobjarrlb); Pmil.emit (opcodes.ldc_i4_1); Pmil.emit (Opcodes.ldloc, Y1LB); PMIL.Emit (OpCodes.box, TypeOf (int)); PMIL.emit (Opcodes .Stelem_ref; Pmil.emit (OpCodes.ldloc, Tempobjarrlb); Pmil.emit (OpCodes.ldc_i4_2); PMIL.Emit (Opcodes.ldloc, X2LB); Pmil.Emit (OpCodes.box, TypeOf (int)); PMIL . Emit (opcodes.stelem_ref; Pmil.emit (Opcodes.ldloc, Tempobjarrlb); Pmil.emit (OpCodes.ldc_i4_3); Pmil.emit (Opcodes.ldloc, Y2LB); Pmil.emit (OpCodes.box, Typeof )); PMIL.Emit (opcodes.stelem_ref); Pmil.emit (Opcodes.ldloc, Tempobjarrlb); Pmil.emit (opcodes.ldc_i4_4); Pmil.emit (Opcodes.ldloc, Point1lb); Pmil.emit (opcodes.ldloc Point2lb); Pmil.emitcall (OpCodes.callvirt, Pointdpbldr, NULL); Pmil.emit (Opcodes.box, TypeOf (int)); Pmil.emit (Opcodes.Stelem_ref); Pmil.emit (Opcodes.ldloc, Tempobjarrlb) Pmil.emitcall (opcodes.call, writelinemi, null); Pmil.emit (opcodes.ret); console.writeline ("PointMain (entry Point ) Built. "); PointType = myTypeBldr.CreateType (); Console.WriteLine (" Type baked "); myAsmBldr.SetEntryPoint (pointMainBldr); myAsmBldr.Save (asmFileName); Console.WriteLine (". Assembly saved as' {0 } '. ", asmFileName); Console.WriteLine (" Type' {0} 'at the prompt to run your new " " dynamically generated dot product calculator ", asmFileName);. // After execution, this program will have generated And Written to Disk, // in The Directory you Executed It from, a program named //.exe. You can run it by Typing

// the name you Gave it During Execution, in The Same Directory Where

// You Executed this Program.Return PointType;

}

Public static void main () {

TYPE mytype = builddyNassembly ();

Console.writeline ("---");

// let's invoke the Type 'Point' created IN Our Dynamic Assembly.

Object Ptinstance = Activator.createInstance (Mytype, New Object [] {0,0});

Mytype.invokemember ("Pointmain",

BindingFlags.InvokeMethod,

NULL,

Ptinstance,

New Object [0]);

}

}

转载请注明原文地址:https://www.9cbs.com/read-114849.html

New Post(0)