我通过COM互操作获得的“对象[*]”类型是什么? [英] What is this `Object[*]` type I get with COM interop?

查看:83
本文介绍了我通过COM互操作获得的“对象[*]”类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行C#excel互操作。我从C#调用宏,并且期望对象数组。我可以从返回二维数组的宏中获取二维对象数组。

I do C# excel interop. I call macros from C#, and I expect arrays of objects. I'm able to get 2-dimensional arrays of objects from the macros which returns 2-dimensional arrays.

但是,另一个(第三方)宏应该返回一维数组。我无法使(object [])xlApp.Run(...)工作(抛出异常),并且调试器中的类型信息说明了结果类型为 Object [*] 。来自异常的实际消息是

However, an other (third party) macro is supposed to return a one-dimensional array. I can't get the (object[])xlApp.Run(...) working (it throws an exception), and the type info in the debugger says the result is of type Object[*]. The actual message from the exception is

Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'.

这是什么 Object [*] 类型以及如何从中检索一维数组?

What is this Object[*] type and how do I retrieve a one-dimensional array from this ?

编辑:在我看来,这可能意味着存在SAFEARRAY的VARIANTS。但是,接下来出现两个问题:为什么二维数组一切正常?如何将SAFEARRAY转换为C#数组?

EDIT: It occurred to me that this could mean a SAFEARRAY of VARIANTS. But then, two questions arise: why is everything ok with 2-dimensional arrays ? How do I convert a SAFEARRAY to a C# array ?

推荐答案

我对有关您的问题的各种文章感兴趣:

I foulnd various articles about your problem :

OPCFondation:基本上不是声明它作为对象数组,您可以将其声明为Array而不提供任何元素类型。因此,不要强制转换Object [],而是强制转换为Array,并使用foreach循环来使用子数组。

OPCFondation : Basically instead of declaring it as an array of objects, you can just declare it as an Array without providing any element type. So do not cast in Object[] but Array, and use a foreach loop to use the subarray.

foreach(object subobject in (Array)myarrayitem)
{
   //do stuff with the subobject, even browse further
}

此解决方案似乎可以正常工作,因为您可以再次找到它此处

This solution seems to work since you can find it again here.

在StackOverflow 上:他们谈论的是下界> 0的数组,它为您提供了Object [*]类型,并带有一些可能对该主题感兴趣的链接,但我认为第一个想法是好的一个。

On StackOverflow : they speak about arrays with lower bound > 0, which gives you the Object[*] type, with some links that can be interesting about the subject, but I think the first idea is the good one.

这篇关于我通过COM互操作获得的“对象[*]”类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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