diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index eed86f3..c6eec3e 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using ICD.Common.Properties; @@ -932,6 +933,30 @@ namespace ICD.Common.Utils.Extensions return array; } + /// + /// Creates a generic List of the given item type. + /// + /// + /// + /// + public static IList ToList([NotNull] this IEnumerable extends, [NotNull] Type itemType) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (itemType == null) + throw new ArgumentNullException("itemType"); + + Type genericListType = typeof(List<>); + Type specificListType = genericListType.MakeGenericType(itemType); + IList list = (IList)ReflectionUtils.CreateInstance(specificListType); + + foreach (object item in extends) + list.Add(item); + + return list; + } + /// /// Optimized ToList implementation with fewer allocations. ///