I use Reflection.Emitto create exe. I have come so far that I can create a working CIL PE. (It just prints a line in Console.WriteLine.) But the argument of the main method is automatically generated (A_0).
.method public static void Main(string[] A_0) cil managed
{
.entrypoint
.maxstack 1
IL_0000: nop
IL_0001: ldstr "Cafe con pan"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: ret
}
compare it with the code from the corresponding program in C #
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Cafe con pan"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ret
}
the argument name is args. How can I name the argument name? The code I use to create the method is as follows:
Il = System.reflection.Emit
Re = System.Reflection
tb = Reflection.Emit.TypeBuilder
Il.MethodBuilder meth = tb.DefineMethod(
"Main",
Re.MethodAttributes.Public | Re.MethodAttributes.Static,
typeof(void),
new Type[] { typeof(String[]) });
Il.ILGenerator methIL = meth.GetILGenerator();
methIL.Emit(Il.OpCodes.Nop);
methIL.Emit(Il.OpCodes.Ldstr, "Cafe con pan");
Type [] args = new Type []{typeof(string)};
Re.MethodInfo printString = typeof(Console).GetMethod("WriteLine", args);
methIL.Emit(Il.OpCodes.Call, printString);
methIL.Emit(Il.OpCodes.Ret);
I checked the documentation TypeBuilder.DefineMethodfor any clue to do this, as it is a logical place to have such information, but to no avail.
Anyone have a suggestion?
source
share