What is this `Object[*]` type I get with COM interop?

青春壹個敷衍的年華 提交于 2019-12-01 03:39:27
LaGrandMere

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.

sikalvag

Use

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

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
)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!