我用VC的atl定一个一个接口,有两个数组求和的函数,idl里面的定义如下:interface IBar : IDispatch{
    [id(1)] HRESULT Sum([in] short sArray[5],[out,retval]long *pVal);
    [id(2)] HRESULT Sum2([in] short nArraySize, [in,size_is(nArraySize)] short sArray[], [out,retval] long* pVal);我用Object Browser看Sum()函数的定义都是我期待的int Sum(short[] sArray)。
Sum函数的用法没有疑问,如下的C#调用,传一个更大的数组给它也没有问题。            short[] array=new short[6]{7,2,3,4,5,6};
            IBar p=new Bar();
            Console.WriteLine( p.Sum(array) );但是如果调用Sum2,把数组及其大小传给它当参数,就会有问题了:Console.WriteLine(p.Sum2(Convert.ToSByte(array.Length), array));编译提示错误:
1>error CS1502: The best overloaded method match for 'MyLib.IBar.Sum2(short, ref short)' has some invalid arguments
1>error CS1503: Argument 2: cannot convert from 'short[]' to 'ref short'
很奇怪,我分明是声明了一个数组作为参数的,C#程序Add reference的时候的Interop竟然将其识别为ref short,也就是我如果把Sum2函数的调用改成下面这样就能编译通过了:short a=2;
Console.WriteLine(p.Sum2(Convert.ToSByte(array.Length), ref a));但是这并不是我定义的Sum2函数的本意啊。
---------------------------------------------------------------
这到底是为什么呢? 为什么Sum2么有被识别为Sum2(short, short[]呢)?