.NET 4.0 Excel Interop issues with dynamic collections

核能气质少年 提交于 2019-11-28 11:32:51

There are two distinct kind of arrays in .NET, a one dimensional 'vector' and multidimensional arrays. You got the latter back, a multidimensional array with a rank of 1. This will happen if the unmanaged code has returned a SAFEARRAY whose lower-bound isn't 0.

You can read the content of the array with Array.GetValue(). Or convert it, like this:

    private static object[] ConvertArray(Array arr) {
        int lb = arr.GetLowerBound(0);
        var ret = new object[arr.GetUpperBound(0) - lb + 1];
        for (int ix = 0; ix < ret.Length; ++ix) {
            ret[ix] = arr.GetValue(ix + lb);
        }
        return ret;
    }

Test:

    var native = Array.CreateInstance(typeof(object), new int[] { 42 }, new int[] { 1 });
    var dotnet = ConvertArray(native);

NOTE: you may have a problem in .NET 4.0 and up when you some COM type libraries, Office in particular. The property or method may return a variant that contains an array. Ends up as dynamic in your C# program. The C# compiler does not generate the proper binder code in that case. Work around that by casting first to (object), then to (Array).

The answer above is useful but does not address the issue of how to cast to Array.

Under .Net versions before 4.0 a simple cast would work.

in C# 4.0 one must use

System.Array a = (System.Array)((object)  returnedObject ); // note order of brackets

see http://blogs.msdn.com/b/mshneer/archive/2010/06/01/oh-that-mysteriously-broken-visiblesliceritemslist.aspx

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