我通过 COM 互操作获得的“Object[*]”类型是什么?
我做 C# excel 互操作。我从 C# 调用宏,并且期望对象数组。我能够从返回二维数组的宏中获取对象的二维数组。
但是,其他(第三方)宏应该返回一维数组。我无法让 (object[])xlApp.Run(...)
工作(它引发异常),并且调试器中的类型信息表明结果的类型为 对象[*]
。来自异常的实际消息是
Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'.
这个 Object[*]
类型是什么以及如何从中检索一维数组?
编辑:我想到这可能意味着 VARIANTS 的 SAFEARRAY。但随后出现了两个问题:为什么二维数组就一切正常?如何将 SAFEARRAY 转换为 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.
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[]'.
What is this Object[*]
type and how do I retrieve a one-dimensional array from this ?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我犯了关于你的问题的各种文章:
OPCFondation : 基本上而不是将其声明为对象数组,您可以将其声明为数组而不提供任何元素类型。因此,不要将 Object[] 转换为 Array,并使用 foreach 循环来使用子数组。
该解决方案似乎有效,因为您可以在此处再次找到它。
在 StackOverflow 上:他们谈论下界 > 的数组0,它为您提供了 Object[*] 类型,其中包含一些有关该主题的有趣链接,但我认为第一个想法是好的。
I foulnd various articles about your problem :
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.
This solution seems to work since you can find it again here.
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.
使用
Use
这真是一个皮塔饼。我在两个项目中都有这段代码:
一个有效,另一个无效。区别。工作中的一个目标是 .Net 2.0,另一个目标是 .Net 4.0,但出现错误:
事实证明,如果将本文的 Visual Studio 版本从 VS2005 更改为 VS2010,LinkSources 数据类型就会发生变化。
MSDN Workbook.LinkSources方法
VS2010:
VS2005:
This was such a PITA. I had this code in two projects:
One worked the other did not. The Difference. Working one was targeting .Net 2.0 and other one was .Net 4.0 that was giving the error:
Turns out if you change the Visual Studio version of this article from VS2005 to VS2010 the LinkSources datatype changes.
MSDN Workbook.LinkSources Method
VS2010:
VS2005: