Kamis, 15 November 2007

Code Generator Example


// Set up our assembly and module builders:


string outFilename = "foo.exe";


AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(


new AssemblyName("foo"), AssemblyBuilderAccess.Save,


AppDomain.CurrentDomain.Evidence);


ModuleBuilder mb = ab.DefineDynamicModule(ab.FullName, outFilename, false);


// Create a simple type with one method:


TypeBuilder tb = mb.DefineType("Program",


TypeAttributes.Public | TypeAttributes.Sealed, typeof(object));


MethodBuilder m = tb.DefineMethod("MyMethod",


MethodAttributes.Public | MethodAttributes.Static);


// Now emit some very simple "Hello World" code:


ILGenerator ilg = m.GetILGenerator();


ilg.Emit(OpCodes.Ldstr, "Hello, World!");


ilg.Emit(OpCodes.Call,


typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));


ilg.Emit(OpCodes.Ret);


// Lastly, create the type, set our entry point, and save to disk:


tb.CreateType();


ab.SetEntryPoint(m);


ab.Save(outFilename);


 


For all of you who have example on LGC for 2.0 Please Post!!!

Tidak ada komentar: