This is a discussion on Reflection.Emit: Basic question about DefineDynamicModule() - DOTNET ; Hi, In the program below there is two instances of the string "output.exe" and if I change the latter one to "output1.exe" the program breaks (the generated binary will crash on execution throwing an MissingMethodException). I really don't get this. ...
Hi,
In the program below there is two instances of the string "output.exe" and
if I change the latter one to "output1.exe" the program breaks (the generated
binary will crash on execution throwing an MissingMethodException).
I really don't get this. It doesn't seem like the string parameter of
DefineDynamicModule() was meant to be a filename?! See for instance the
sample code here:
http://msdn2.microsoft.com/en-us/library/ht55atse.aspx
And in this overload:
http://msdn2.microsoft.com/en-us/library/ht55atse.aspx
You specify both filename and module name.
So why can't I pass in for instance "output" instead of "output.exe" to
..DefineDynamicModule() ?? It seems redundant/weird/ugly to have to input the
filename twice?
static void Main(string[] args)
{
AssemblyName assemblyName = new AssemblyName("TestAssembly");
AssemblyBuilder asmBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName,
AssemblyBuilderAccess.RunAndSave);
ModuleBuilder moduleBuilder =
asmBuilder.DefineDynamicModule("output.exe");
TypeAttributes attributes = TypeAttributes.Public | TypeAttributes.Class;
TypeBuilder typeBuilder = moduleBuilder.DefineType("Program",
attributes, null);
MethodBuilder methodBuilder = typeBuilder.DefineMethod("Main",
MethodAttributes.Public | MethodAttributes.Static, typeof(void), null);
ILGenerator ilGenerator = methodBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("Hello World");
ilGenerator.Emit(OpCodes.Ret);
Type generatedType = typeBuilder.CreateType();
asmBuilder.SetEntryPoint(methodBuilder, PEFileKinds.ConsoleApplication);
asmBuilder.Save("output.exe");
Console.WriteLine("assembly generated. press the any key to exit.");
Console.ReadKey();
}