c# - 从 C# 调用 ILAsm vararg 函数

标签 c# .net 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
    }
}

我的 dll 编译成功,但我无法从 C# 代码调用此函数。当我用

调用它时
var k = TestSentinel.Sum(1);

我收到错误信息:

Error  The best overloaded method match for 'TestSentinel.Sum(__arglist, ...)' has some invalid arguments

对于任何其他数量的参数,我都会收到 wrong argument number 消息。 调用我的 ILAsm 函数的正确方法是什么?

最佳答案

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

 var k = TestSentinel.Sum(__arglist(1));
 var l = TestSentinel.Sum(__arglist(1, 2, 3));

这不是很有用,最好关注参数数组。

关于c# - 从 C# 调用 ILAsm vararg 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19268347/

相关文章:

c# - 使用 ControllerFeatureProvider 禁止特定 ASP.NET Core Controller 在不同主机中可用

c# - 附加文本不起作用

c# - 目标机主动拒绝导致无法连接?

.net - 将数据从一个数据集循环复制到另一个数据集的最快方法

c# - 强制子类初始化一个变量

c# - 托管和非托管

C# 处理 IDisposable

c# - 如何在 C# 中获取屏幕上文本的边界框?

c# - C# 和 Java 之间的主要区别是什么?

c# - ----s 在 StringBuilder.ToString() 的上下文中是什么意思?