Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast System.Object[*] to System.Object[]

When I Tried to return an Array in VFP9 language COM/DLL to my .NET C# project I receive a System.Object[*] array and I can not cast to System.Object[] (Without asterisk).

like image 805
Cheva Avatar asked Sep 16 '10 22:09

Cheva


1 Answers

Timwi's solution should work fine. You can do something a bit simpler using Linq:

object[] newArray = sourceArray.Cast<object>().ToArray();

In case you need to recreate a System.Object[*] to pass it back to VFP, you can use this overload of the Array.CreateInstance method:

public static Array CreateInstance(
    Type elementType,
    int[] lengths,
    int[] lowerBounds
)

You can use it as follows:

object[] normalArray = ...

// create array with lower bound of 1
Array arrayStartingAt1 =
    Array.CreateInstance(
        typeof(object),
        new[] { normalArray.Length },
        new[] { 1 });

Array.Copy(normalArray, 0, arrayStartingAt1, 1, normalArray.Length);
like image 54
Thomas Levesque Avatar answered Oct 09 '22 16:10

Thomas Levesque