boring91
boring91

Reputation: 1121

C# - Calling a Delegate with 'params string[]' from a Dynamic Method

Is it possible to call a delegate that takes as a parameter params string[] from a dynamic method?

Example:

delegate string Del(params string[] arg);
...
Del f = args => "Called successfully.";
...
var mb = typeBuilder.DefineMethod(
                    "MyMethod",
                    MethodAttributes.Public | MethodAttributes.Virtual,
                    typeof(string),
                    new Type[] {typeof(string), typeof(string)});
...
var generator = mb.GetILGenerator();
...
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Ldarg_2);
...
generator.Emit(OpCodes.Call, f.GetType().GetMethod("Invoke"));

Because whenever I run the code above, it gives me this:

Unhandled Exception: System.InvalidProgramException: JIT Compiler encountered an internal limitation.

Am I doing something wrong here?

Upvotes: 1

Views: 70

Answers (1)

TcKs
TcKs

Reputation: 26642

You must pass the array of strings. The "params" keyword is just for syntactic sugar in c#/vb but is ignored in IL.

Upvotes: 0

Related Questions