COM 组件公开了一个 API,该 API 需要一个对象类型的 ref 参数。根据此 API 的文档,它将使用值数组填充 ref 对象。现在我的问题出在产品环境中,我无法预测我将返回的元素数量。
以下代码将起作用。
COMClass objCOM = new COMClass ();
object colOfInts= new int[10]; // What if I don't know the following will return array of size 10?
int errorcode = objCOM.FillThisIn(ref colOfInts);
但是如果我不知道 API 在 ref 中返回的数组的大小怎么办。
在这里更新
object colOfInts = null;
int errorcode = objCOM .FillThisIn(ref colOfInts);
现在,当我检查类型时,我得到 System.Int32[*]
基本上我需要遍历这个数组并检查元素是否存在
最佳答案
您正在返回一个下限不为 0 的数组。这在 COM 互操作中并不少见,下一个可能的选择是 1。您不必复制它,您可以使用 Array.GetValue( ). Array.GetLowerBound() 告诉您从哪里开始,Array.GetLength() 或 Array.GetUpperBound() 告诉您要走多远。
关于c# - Ref 参数返回未知大小的数组。如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11227314/