-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
DynamicEnumerableExtensions.cs
135 lines (123 loc) · 5.16 KB
/
DynamicEnumerableExtensions.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Dynamic.Core.Validation;
using System.Reflection;
namespace System.Linq.Dynamic.Core
{
/// <summary>
/// Define extensions on <see cref="IEnumerable"/>.
/// </summary>
public static class DynamicEnumerableExtensions
{
private static readonly MethodInfo ToDynamicArrayGenericMethod;
static DynamicEnumerableExtensions()
{
ToDynamicArrayGenericMethod = typeof(DynamicEnumerableExtensions).GetTypeInfo().GetDeclaredMethods("ToDynamicArray")
.First(x => x.IsGenericMethod);
}
/// <summary>
/// Creates an array of dynamic objects from a <see cref="IEnumerable"/>.
/// </summary>
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
/// <returns>An array that contains the elements from the input sequence.</returns>
#if NET35
public static object[] ToDynamicArray([NotNull] this IEnumerable source)
{
Check.NotNull(source, nameof(source));
return CastToArray<object>(source);
}
#else
public static dynamic[] ToDynamicArray([NotNull] this IEnumerable source)
{
Check.NotNull(source, nameof(source));
return CastToArray<dynamic>(source);
}
#endif
/// <summary>
/// Creates an array of dynamic objects from a <see cref="IEnumerable"/>.
/// </summary>
/// <typeparam name="T">The generic type.</typeparam>
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
/// <returns>An Array{T} that contains the elements from the input sequence.</returns>
public static T[] ToDynamicArray<T>([NotNull] this IEnumerable source)
{
Check.NotNull(source, nameof(source));
return CastToArray<T>(source);
}
/// <summary>
/// Creates an array of dynamic objects from a <see cref="IEnumerable"/>.
/// </summary>
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
/// <param name="type">A <see cref="Type"/> cast to.</param>
/// <returns>An Array that contains the elements from the input sequence.</returns>
#if NET35
public static object[] ToDynamicArray([NotNull] this IEnumerable source, [NotNull] Type type)
#else
public static dynamic[] ToDynamicArray([NotNull] this IEnumerable source, [NotNull] Type type)
#endif
{
Check.NotNull(source, nameof(source));
Check.NotNull(type, nameof(type));
IEnumerable result = (IEnumerable)ToDynamicArrayGenericMethod.MakeGenericMethod(type).Invoke(source, new object[] { source });
#if NET35
return CastToArray<object>(result);
#else
return CastToArray<dynamic>(result);
#endif
}
/// <summary>
/// Creates a list of dynamic objects from a <see cref="IEnumerable"/>.
/// </summary>
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
/// <returns>A List that contains the elements from the input sequence.</returns>
#if NET35
public static List<object> ToDynamicList([NotNull] this IEnumerable source)
#else
public static List<dynamic> ToDynamicList([NotNull] this IEnumerable source)
#endif
{
Check.NotNull(source, nameof(source));
#if NET35
return CastToList<object>(source);
#else
return CastToList<dynamic>(source);
#endif
}
/// <summary>
/// Creates a list of dynamic objects from a <see cref="IEnumerable"/>.
/// </summary>
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
/// <param name="type">A <see cref="Type"/> cast to.</param>
/// <returns>A List that contains the elements from the input sequence.</returns>
#if NET35
public static List<object> ToDynamicList([NotNull] this IEnumerable source, [NotNull] Type type)
#else
public static List<dynamic> ToDynamicList([NotNull] this IEnumerable source, [NotNull] Type type)
#endif
{
Check.NotNull(source, nameof(source));
Check.NotNull(type, nameof(type));
return ToDynamicArray(source, type).ToList();
}
/// <summary>
/// Creates a list of dynamic objects from a <see cref="IEnumerable"/>.
/// </summary>
/// <typeparam name="T">Generic Type</typeparam>
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
/// <returns>A List{T} that contains the elements from the input sequence.</returns>
public static List<T> ToDynamicList<T>([NotNull] this IEnumerable source)
{
Check.NotNull(source, nameof(source));
return CastToList<T>(source);
}
internal static T[] CastToArray<T>(IEnumerable source)
{
return source.Cast<T>().ToArray();
}
internal static List<T> CastToList<T>(IEnumerable source)
{
return source.Cast<T>().ToList();
}
}
}