背景:Windows 7、ExcelDNA 0.30、.NET 4.0
我仍在尝试通过 ExcelDNA 在 Excel 中使用 params/ParamArray 方法。通过使用可变参数,我避免与 System.ParamArrayAttribute 有任何关系,并使用 System.ArgIterator 寻求路径。
可悲的事实是以下编译但仍然不起作用。我不断收到价值错误。出了点问题,但我对这个汇编器的了解还不够多,无法弄清楚。任何想法,任何人?
.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
}
}
}