//fast path, that implementation detail does not really need to be API private static Array ArrayFromCollection(Type elementType, ICollection collection) { if (null == elementType) { throw new ArgumentNullException("elementType"); } if (null == collection) { throw new ArgumentNullException("collection"); } Array array = Array.CreateInstance(elementType, collection.Count); if (RuntimeServices.IsPromotableNumeric(Type.GetTypeCode(elementType))) { int i = 0; foreach (object item in collection) { object value = RuntimeServices.CheckNumericPromotion(item).ToType(elementType, null); array.SetValue(value, i); ++i; } } else { collection.CopyTo(array, 0); } return(array); }
public static Array array(Type elementType, IEnumerable enumerable) { if (null == elementType) { throw new ArgumentNullException("elementType"); } if (null == enumerable) { throw new ArgumentNullException("enumerable"); } #pragma warning disable 618 //obsolete ICollection collection = enumerable as ICollection; if (null != collection) //fast path { return(ArrayFromCollection(elementType, collection)); } #pragma warning restore 618 List l = null; if (RuntimeServices.IsPromotableNumeric(Type.GetTypeCode(elementType))) { l = new List(); foreach (object item in enumerable) { object value = RuntimeServices.CheckNumericPromotion(item).ToType(elementType, null); l.Add(value); } } else { l = new List(enumerable); } return(l.ToArray(elementType)); }
public static Array array(Type elementType, IEnumerable enumerable) { if (null == enumerable) { throw new ArgumentNullException("enumerable"); } if (null == elementType) { throw new ArgumentNullException("elementType"); } // future optimization, check EnumeratorItemType of enumerable // and get the fast path whenever possible List l = null; if (RuntimeServices.IsPromotableNumeric(Type.GetTypeCode(elementType))) { l = new List(); foreach (object item in enumerable) { object value = RuntimeServices.CheckNumericPromotion(item).ToType(elementType, null); l.Add(value); } } else { l = new List(enumerable); } return(l.ToArray(elementType)); }