参考参数返回未知大小的数组。如何处理?数组、如何处理、大小、参数

2023-09-05 00:49:24 作者:梦想启程

一个COM组件公开的API,它预计,对象类型的参考参数。按照这个API的文档,这将填补值的数组裁判对象。现在我的问题是督促ENV我不能predict元素的数量,我会回来。

A COM component exposes an API which expects a ref param of object type. As per the documentation of this API, it will fill the ref object with array of values. Now my problem is in prod env I can't predict the number of elements which I will get back.

随着code会工作。

     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返回的参考。

But what if I don't know the size of array that API returns in ref.

更新此处

        object colOfInts = null;
        int errorcode = objCOM .FillThisIn(ref colOfInts);

现在当我检查的类型我得到System.Int32的[*]

now when I check the type I get System.Int32[*]

基本上,我需要遍历数组和检查元素的presence

Basically I need to iterate through this array and check for the presence of an element

推荐答案

正在找回一个数组,其下限是不是0,这是很常见的COM互操作,下一个可能的选择是1,你不必复制它,您可以访问Array.GetValue的元素()。 Array.GetLowerBound()告诉你哪里开始,Array.GetLength()或Array.GetUpperBound()告诉你能走多远。

You are getting back an array whose lower bound isn't 0. That's not uncommon in COM interop, the next likely choice is 1. You don't have to copy it, you can access the elements with Array.GetValue(). Array.GetLowerBound() tells you where to start, Array.GetLength() or Array.GetUpperBound() tells you how far to go.