我通过 COM 互操作获得的“Object[*]”类型是什么?

发布于 2024-10-09 11:17:54 字数 460 浏览 3 评论 0原文

我做 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ杯具 2024-10-16 11:17:54

我犯了关于你的问题的各种文章:

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

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

该解决方案似乎有效,因为您可以在此处再次找到它。

在 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.

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.

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.

绝影如岚 2024-10-16 11:17:54

使用

System.Array a = (System.Array)((object)  returnedObject );

Use

System.Array a = (System.Array)((object)  returnedObject );
橘和柠 2024-10-16 11:17:54

这真是一个皮塔饼。我在两个项目中都有这段代码:

Workbook wb = null;
try
{
    wb = excel.Workbooks.Open(filePath, false, true, 5, null, "WrongPAssword");
}
catch
{
    return false;
}    

try
{

    Array links = (Array)wb.LinkSources(XlLink.xlExcelLinks);
    if (links != null)
    {

一个有效,另一个无效。区别。工作中的一个目标是 .Net 2.0,另一个目标是 .Net 4.0,但出现错误:

无法将“System.Object[*]”类型的对象转换为类型
'System.Object[]'。

事实证明,如果将本文的 Visual Studio 版本从 VS2005 更改为 VS2010,LinkSources 数据类型就会发生变化。

MSDN Workbook.LinkSources方法

VS2010:

Object LinkSources(
    Object Type
)

VS2005:

public virtual Object LinkSources (
    [OptionalAttribute] Object Type
)

This was such a PITA. I had this code in two projects:

Workbook wb = null;
try
{
    wb = excel.Workbooks.Open(filePath, false, true, 5, null, "WrongPAssword");
}
catch
{
    return false;
}    

try
{

    Array links = (Array)wb.LinkSources(XlLink.xlExcelLinks);
    if (links != null)
    {

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:

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

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:

Object LinkSources(
    Object Type
)

VS2005:

public virtual Object LinkSources (
    [OptionalAttribute] Object Type
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文