Set name in method arguments

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
  // Code size       12 (0xc)
  .maxstack  1
  IL_0000:  nop
  IL_0001:  ldstr      "Cafe con pan"
  IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000b:  ret
} // end of method Program::Main

compare it with the code from the corresponding program in C #

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       13 (0xd)
  .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
} // end of method Program::Main

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", // name
                Re.MethodAttributes.Public | Re.MethodAttributes.Static,
                // method attributes
                typeof(void),   // return type
                new Type[] { typeof(String[]) }); // parameter types

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?

+3
source share
1 answer

It looks like it MethodBuilder.DefineParameterallows you to specify parameter names:

. ParameterBuilder, .

+4

All Articles