L
linkspeed
Hi, All:
I think lots of ppl know "hello, world... Reflection.Emit style!"
Yesterday, I played with this samll program for a while because
my simple emit program didn't work.
After a few times compiling and running, I noticed that if i change
the AssemblyBuilderAccess.RunandSave to AssemblyBuilderAccess.Save,
and
removed the codes that run the new types, the program WON'T work!
Any comments on that?
That program can be found at
http://blogs.msdn.com/joelpob/archive/2004/01/21/61411.aspx
I copy and paste it here:
public class EmitHelloWorld
{
static void Main(string[] args)
{
// create a dynamic assembly and module
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "HelloWorld";
AssemblyBuilder assemblyBuilder =
Thread.GetDomain().DefineDynamicAssembly(assemblyName,
AssemblyBuilderAccess.RunAndSave);
^^^^^^^^^^^^^ change to Save
ModuleBuilder module;
module =
assemblyBuilder.DefineDynamicModule("HelloWorld.exe");
// create a new type to hold our Main method
TypeBuilder typeBuilder =
module.DefineType("HelloWorldType", TypeAttributes.Public |
TypeAttributes.Class);
// create the Main(string[] args) method
MethodBuilder methodbuilder =
typeBuilder.DefineMethod("Main", MethodAttributes.HideBySig |
MethodAttributes.Static | MethodAttributes.Public, typeof(void), new
Type[] { typeof(string[]) });
// generate the IL for the Main method
ILGenerator ilGenerator = methodbuilder.GetILGenerator();
ilGenerator.EmitWriteLine("hello, world");
ilGenerator.Emit(OpCodes.Ret);
// bake it
Type helloWorldType = typeBuilder.CreateType();
// run it
helloWorldType.GetMethod("Main").Invoke(null, new string[]
{null});
^^^^^^^^delete above 4 lines.
// set the entry point for the application and save it
assemblyBuilder.SetEntryPoint(methodbuilder,
PEFileKinds.ConsoleApplication);
assemblyBuilder.Save("HelloWorld.exe");
}
}
Cheers
shane
I think lots of ppl know "hello, world... Reflection.Emit style!"
Yesterday, I played with this samll program for a while because
my simple emit program didn't work.
After a few times compiling and running, I noticed that if i change
the AssemblyBuilderAccess.RunandSave to AssemblyBuilderAccess.Save,
and
removed the codes that run the new types, the program WON'T work!
Any comments on that?
That program can be found at
http://blogs.msdn.com/joelpob/archive/2004/01/21/61411.aspx
I copy and paste it here:
public class EmitHelloWorld
{
static void Main(string[] args)
{
// create a dynamic assembly and module
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "HelloWorld";
AssemblyBuilder assemblyBuilder =
Thread.GetDomain().DefineDynamicAssembly(assemblyName,
AssemblyBuilderAccess.RunAndSave);
^^^^^^^^^^^^^ change to Save
ModuleBuilder module;
module =
assemblyBuilder.DefineDynamicModule("HelloWorld.exe");
// create a new type to hold our Main method
TypeBuilder typeBuilder =
module.DefineType("HelloWorldType", TypeAttributes.Public |
TypeAttributes.Class);
// create the Main(string[] args) method
MethodBuilder methodbuilder =
typeBuilder.DefineMethod("Main", MethodAttributes.HideBySig |
MethodAttributes.Static | MethodAttributes.Public, typeof(void), new
Type[] { typeof(string[]) });
// generate the IL for the Main method
ILGenerator ilGenerator = methodbuilder.GetILGenerator();
ilGenerator.EmitWriteLine("hello, world");
ilGenerator.Emit(OpCodes.Ret);
// bake it
Type helloWorldType = typeBuilder.CreateType();
// run it
helloWorldType.GetMethod("Main").Invoke(null, new string[]
{null});
^^^^^^^^delete above 4 lines.
// set the entry point for the application and save it
assemblyBuilder.SetEntryPoint(methodbuilder,
PEFileKinds.ConsoleApplication);
assemblyBuilder.Save("HelloWorld.exe");
}
}
Cheers
shane