简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM