mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 03:57:32 +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:
parent
e0663bdbfd
commit
5e61098e71
1 changed files with 25 additions and 0 deletions
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue