从C#调用ILAsm vararg函数

use*_*923 1 .net c# clr il

我正在编写ILAsm函数,它接收可变数量的参数并返回它们的总和:

.class public auto ansi TestSentinel
{
    .method public static vararg unsigned int64 Sum( /* all arguments are optional */ )
    {
        .locals init( value class [mscorlib]System.ArgIterator Args, 
                      unsigned int64 Sum, 
                      int32 NumArgs )
        ... 
        ldloc Sum
        ret
    }
}
Run Code Online (Sandbox Code Playgroud)

我的DLL编译成功但我不能用C#代码调用这个函数.我打电话的时候

var k = TestSentinel.Sum(1);
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:

Error  The best overloaded method match for 'TestSentinel.Sum(__arglist, ...)' has some invalid arguments
Run Code Online (Sandbox Code Playgroud)

我收到任何其他数量的论据wrong argument number.调用我的ILAsm函数的正确方法是什么?

Han*_*ant 5

它需要使用未记录的__arglist关键字:

 var k = TestSentinel.Sum(__arglist(1));
 var l = TestSentinel.Sum(__arglist(1, 2, 3));
Run Code Online (Sandbox Code Playgroud)

这不是很有用,而是更好地专注于params数组.