Exemplo n.º 1
0
 public static Array ToArray(ICollection value, Type conversionType, Encoding encoding)
 {
     if (value == null) {
         return null;
     }
     if (conversionType == null) {
         throw new ArgumentNullException("conversionType");
     }
     if (!conversionType.IsArray) {
         throw new ArgumentException("Must be an array type.");
     }
     ArrayList arraylist = new AssocArray(value).toArrayList();
     if (conversionType == typeofObjectArray) {
         return arraylist.ToArray();
     }
     try {
         return arraylist.ToArray(conversionType.GetElementType());
     }
     catch (InvalidCastException) {
         Int32 length = arraylist.Count;
         Type elementType = conversionType.GetElementType();
         Array array = Array.CreateInstance(elementType, length);
         if (array.GetType() != conversionType) {
             throw new RankException("Only single dimension arrays are supported here.");
         }
         for (Int32 i = 0; i < length; i++) {
             array.SetValue(ChangeType(arraylist[i], elementType, encoding), i);
         }
         return array;
     }
 }