Question

I am writing ILAsm function that receives variable number of arguments and returns their sum:

.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
    }
}

My dll compiles successfully but I cannot call this function from C# code. When I call it with

var k = TestSentinel.Sum(1);

I receive error message:

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

And with any other number of arguments I receive wrong argument number message. What is the correct way to call my ILAsm function?

Was it helpful?

Solution

It requires using the undocumented __arglist keyword:

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

This isn't very useful, better focus on a params array instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top