mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Added extension method for dynamically converting a sequence to a generic list of the given item type
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a generic List of the given item type.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="itemType"></param>
|
||||
/// <returns></returns>
|
||||
public static IList ToList<T>([NotNull] this IEnumerable<T> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Optimized ToList implementation with fewer allocations.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user