ArrayDowncasters Implementation for Downcasting from System.Array to Array of Specific Type in C#

There are a few alternative solutions which might work

LINQ

It is possible to use Enumerable.Cast<T>() to achieve the same results (at least for a 1D array)

(TestMethod)
public void CheckConversion()
{
    //    One Dimensional Array with int element convert to int()
    var array1 = Array.CreateInstance(typeof(int), 10);
    for (int i = 0; i < array1.Length; i++)
    {
        array1.SetValue(3, i);
    }

    var intArray1 = ArrayDowncasters.ToIntArray1(array1);
    var intArray2 = array1.Cast<int>().ToArray();
    CollectionAssert.AreEqual(intArray1, intArray2);

    //    One Dimensional Array with double element convert to double()
    var array2 = Array.CreateInstance(typeof(double), 10);
    for (int i = 0; i < array2.Length; i++)
    {
        array2.SetValue(3.1, i);
    }

    var doubleArray1 = ArrayDowncasters.ToDoubleArray1(array2);
    var doubleArray2 = array2.Cast<double>().ToArray();
    CollectionAssert.AreEqual(doubleArray1, doubleArray2);

}

Generics

The naming of the functions e.g. ToDoubleArray1() indicates that there might be 2 or 3 or n dimensional versions to be added. I don’t know if a LINQ solution can be made to work for these but the current code can be re-written as a single generic function

public static T() Convert<T>(Array input)
{
    Type elementType = input.GetType().GetElementType();
    if (input.Rank != 1)
    {
        throw new System.InvalidOperationException();
    }

    if (!elementType.Equals(typeof(T)))
    {
        throw new System.InvalidOperationException();
    }

    var output = new T(input.GetLength(0));
    for (int i = 0; i < input.GetLength(0); i++)
    {
        output(i) = (T)input.GetValue(i);
    }
    return output;
}

which means that a single generic function per dimension may suffice

(TestMethod)
public void CheckConversionGeneric()
{
    //    One Dimensional Array with int element convert to int()
    var array1 = Array.CreateInstance(typeof(int), 10);
    for (int i = 0; i < array1.Length; i++)
    {
        array1.SetValue(3, i);
    }

    var intArray1 = ArrayDowncasters.ToIntArray1(array1);
    var intArray2 = ArrayDowncasters.Convert<int>(array1);
    CollectionAssert.AreEqual(intArray1, intArray2);

    //    One Dimensional Array with double element convert to double()
    var array2 = Array.CreateInstance(typeof(double), 10);
    for (int i = 0; i < array2.Length; i++)
    {
        array2.SetValue(3.1, i);
    }

    var doubleArray1 = ArrayDowncasters.ToDoubleArray1(array2);
    var doubleArray2 = ArrayDowncasters.Convert<double>(array2);
    CollectionAssert.AreEqual(doubleArray1, doubleArray2);

}