-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathListUtil.cs
40 lines (31 loc) · 1.13 KB
/
ListUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GotaSoundIO {
/// <summary>
/// List utility.
/// </summary>
public static class ListUtil {
public static IEnumerable<T> ConvertTo<T>(this IEnumerable items) {
return items.Cast<object>().Select(x => (T)Convert.ChangeType(x, typeof(T)));
}
public static List<T> ConvertToList<T>(this IEnumerable items) {
return items.ConvertTo<T>().ToList();
}
public static IList ConvertToList(this IEnumerable items, Type targetType) {
var method = typeof(ListUtil).GetMethod(
"ConvertToList",
new[] { typeof(IEnumerable) });
var generic = method.MakeGenericMethod(targetType);
return (IList)generic.Invoke(null, new[] { items });
}
public static T[] SubArray<T>(this T[] data, int index, int length) {
T[] result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
}
}