Context: Windows 7, ExcelDNA 0.30, .NET 4.0
I'm still trying to get a params/ParamArray approach working in Excel via ExcelDNA. By using varags, I'm avoiding anything to do with System.ParamArrayAttribute and pursuing a path with System.ArgIterator.
The sad fact is that the following compiles but still doesn't work. I keep getting value errors. Something's amiss but I don't know enough of this assembler (yet) to figure it out. Any ideas, anyone??
.assembly extern mscorlib { }
.assembly Test {}
.module test.dll
.namespace VTest {
.class public Test {
// Compute sum of undefined number of arguments
.method public static vararg int64 IntSum(/* all arguments optional */)
{
.locals init(valuetype [mscorlib]System.ArgIterator Args,
unsigned int64 Sum,
int32 NumArgs)
ldc.i8 0
stloc Sum
ldloca Args
arglist // Create argument list structure
// Initialize ArgIterator with this structure:
call instance void [mscorlib]System.ArgIterator::.ctor(
value class [mscorlib]System.RuntimeArgumentHandle)
// Get the optional argument count:
ldloca Args
call instance int32 [mscorlib]System.ArgIterator::GetRemainingCount()
stloc NumArgs
// Main cycle:
LOOP:
ldloc NumArgs
brfalse RETURN // if(NumArgs == 0) goto RETURN;
// Get next argument:
ldloca Args
call instance typedref [mscorlib]System.ArgIterator::GetNextArg()
// Interpret it as unsigned int64:
refanyval [mscorlib]System.UInt64
ldind.u8
// Add it to Sum:
ldloc Sum
add
stloc Sum // Sum += *((int64*)&next_arg)
// Decrease NumArgs and go for next argument:
ldloc NumArgs
ldc.i4.m1
add
stloc NumArgs
br LOOP
RETURN:
ldloc Sum
ret
}
}
}
Excel-DNA (and the Excel C API) currently does not support (version 0.30) the 'params' optional parameters. See this discussion for more details and possible workarounds: http://exceldna.codeplex.com/discussions/406719