CSharp examples for System:Array Convert
Converts object to a double array
using System.Runtime.InteropServices; using System.Reflection; using System.Text; using System.Linq; using System.Collections.Generic; using System.Collections; using System;// w w w. j a v a2 s .c o m public class Main{ /// <summary> /// Converts object to a /// </summary> /// <param name="generalArray"></param> /// <returns></returns> public unsafe static Array ToDoubleArray(object generalArray) { Type arrayType = generalArray.GetType(); Array managedArray = null; #region ILArray if (arrayType.Name == "ILArray`1") { dynamic dynamicArray = generalArray; if ((dynamicArray.Dimensions.NumberOfDimensions == 1) || ((dynamicArray.Dimensions.NumberOfDimensions == 2) && (dynamicArray.Dimensions[1] == 1))) { int n = dynamicArray.Dimensions[0]; managedArray = new double[n]; double[] managedArrayCast = managedArray as double[]; for (int i = 0; i < n; ++i) managedArrayCast[i] = dynamicArray.GetValue(i); return managedArray; } else if (dynamicArray.Dimensions.NumberOfDimensions == 2) { int n1 = dynamicArray.Dimensions[0]; int n2 = dynamicArray.Dimensions[1]; managedArray = new double[n1, n2]; double[,] managedArrayCast = managedArray as double[,]; for (int i = 0; i < n1; ++i) { for (int j = 0; j < n2; ++j) { managedArrayCast[i, j] = dynamicArray.GetValue(i, j); } } return managedArray; } throw new Exception("Array must be one or two dimensionsal."); } #endregion else if (arrayType.Name == "ndarray") { managedArray = ManagedArrayFromNumpyArray(generalArray); } else { // not Array, ILArray or Numpy array int[] dimensions = GeneralArray.GetDimensions(generalArray); if (dimensions.Length > 2) throw new Exception("More than two dimensions found."); if (dimensions.Length == 1) { var enumerable = generalArray as IEnumerable<object>; double[] tempArray = new double[enumerable.Count()]; int index = 0; foreach (object item in enumerable) { tempArray[index] = Convert.ToDouble(item); index++; } managedArray = tempArray; } else { var parent = generalArray as IEnumerable<object>; int minLength = int.MaxValue; int maxLength = int.MinValue; int count = parent.FastCount(); foreach (IEnumerable<object> child in parent) { if (child == null) minLength = 0; else { minLength = Math.Min(minLength, child.FastCount()); maxLength = Math.Max(maxLength, child.FastCount()); } } int i = 0; double[,] tempArray = new double[count, maxLength]; foreach (IEnumerable<object> child in parent) { int j = 0; foreach (object element in child) { tempArray[i, j] = Convert.ToDouble(element); j++; } while (j < maxLength) { tempArray[i, j] = Double.NaN; ++j; } i++; } managedArray = tempArray; } } return managedArray; } public unsafe static int[] GetDimensions(object generalArray) { int[] dimensions = null; Type arrayType = generalArray.GetType(); if (generalArray is Array) { Array array = generalArray as Array; dimensions = new int[array.Rank]; for (int i = 0; i < array.Rank; ++i) dimensions[i] = array.GetLength(i); } else if (arrayType.Name == "ndarray") { dynamic dynamicArray = generalArray; dimensions = new int[dynamicArray.Dims.Length]; for (int i = 0; i < dimensions.Length; ++i) dimensions[i] = (int)dynamicArray.Dims[i]; } // for jagged lists or enumerables else if (generalArray is IEnumerable<object>) { dimensions = DimensionsOfJaggedIEnumerable(generalArray as IEnumerable<object>); } return dimensions; } }