/// <summary> /// Creates an n-dimensional array from an ArrayList of ArrayLists or /// a 1-dimensional array from something else. /// </summary> /// <param name="list"><see cref="ArrayList"/> to convert</param> /// <returns><see cref="Array"/> produced.</returns> private static Array ToArray(ArrayList list, Type elementType) { //First we need to work out how many dimensions we're dealing with and what size each one is. //We examine the arraylist we were passed for it's count. Then we recursively do that to //it's first item until we hit an item that isn't an ArrayList. //We then find out the type of that item. List <int> dimensions = new List <int>(); object item = list; for (ArrayList itemAsList = item as ArrayList; item is ArrayList; itemAsList = (item = itemAsList[0]) as ArrayList) { if (itemAsList != null) { int dimension = itemAsList.Count; if (dimension == 0) { return(Array.CreateInstance(elementType, 0)); } dimensions.Add(dimension); } } if (dimensions.Count == 1) //1-dimension array so we can just use ArrayList.ToArray() { return(list.ToArray(elementType)); } //Get an IntSetIterator to hold the position we're setting. IntSetIterator isi = new IntSetIterator(dimensions); //Create our array. Array ret = Array.CreateInstance(elementType, isi.Bounds); //Get each item and put it in the array at the appropriate place. foreach (object val in RecursiveArrayListEnumeration(list)) { ret.SetValue(val, ++isi); } return(ret); }
/// <summary> /// Creates an n-dimensional array from an ArrayList of ArrayLists or /// a 1-dimensional array from something else. /// </summary> /// <param name="list"><see cref="ArrayList"/> to convert</param> /// <param name="elementType">Type of the elements in the list</param> /// <returns><see cref="Array"/> produced.</returns> private static Array ToArray(ArrayList list, Type elementType) { //First we need to work out how many dimensions we're dealing with and what size each one is. //We examine the arraylist we were passed for it's count. Then we recursively do that to //it's first item until we hit an item that isn't an ArrayList. //We then find out the type of that item. List<int> dimensions = new List<int>(); object item = list; for (ArrayList itemAsList = item as ArrayList; item is ArrayList; itemAsList = (item = itemAsList[0]) as ArrayList) { if (itemAsList != null) { int dimension = itemAsList.Count; if (dimension == 0) { return Array.CreateInstance(elementType, 0); } dimensions.Add(dimension); } } if (dimensions.Count == 1) //1-dimension array so we can just use ArrayList.ToArray() { return list.ToArray(elementType); } //Get an IntSetIterator to hold the position we're setting. IntSetIterator isi = new IntSetIterator(dimensions); //Create our array. Array ret = Array.CreateInstance(elementType, isi.Bounds); //Get each item and put it in the array at the appropriate place. foreach (object val in RecursiveArrayListEnumeration(list)) { ret.SetValue(val, ++isi); } return ret; }