参考参数返回未知大小的数组。如何处理? [英] Ref param returning array of unknown size. How to handle?

查看:164
本文介绍了参考参数返回未知大小的数组。如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个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.

这篇关于参考参数返回未知大小的数组。如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆