If I decompile the Test2 constructor:
public class Test2 : VarArgTest
{
public Test2() : base("foo", __arglist("one", 2))
{
}
}
public class VarArgTest
{
public VarArgTest(string test, __arglist)
{
}
}
I get this IL:
IL_0000: ldarg.0
IL_0001: ldstr "foo"
IL_0006: ldstr "one"
IL_000b: ldc.i4.2
IL_000c: call instance vararg void VarargsTest.VarArgTest::.ctor(string,
...,
string,
int32)
I'm trying to generate the same IL stream using the ILGenerator but EmitCall only takes a MethodInfo not a ConstructorInfo and the only Emit overload that takes a ConstructorInfo has no support for passing in additional parameter types.
Okay, After reading this post
I discovered an incredibly easier method of doing this: You can get a token for the constructor with optional arguments from your method builder. Why this is so unbelievably undocumented is a mystery. A similar version of the program in my previous answer is below that does the same thing but with just using this get methodtoken api. This is much easier!