2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
4
using System . Linq . Dynamic . Core . Validation ;
5
+ using System . Reflection ;
5
6
6
7
namespace System . Linq . Dynamic . Core
7
8
{
@@ -10,25 +11,37 @@ namespace System.Linq.Dynamic.Core
10
11
/// </summary>
11
12
public static class DynamicEnumerableExtensions
12
13
{
14
+ private static readonly MethodInfo ToDynamicArrayGenericMethod ;
15
+
16
+ static DynamicEnumerableExtensions ( )
17
+ {
18
+ ToDynamicArrayGenericMethod = typeof ( DynamicEnumerableExtensions ) . GetTypeInfo ( ) . GetDeclaredMethods ( "ToDynamicArray" )
19
+ . First ( x => x . IsGenericMethod ) ;
20
+ }
21
+
13
22
/// <summary>
14
23
/// Creates an array of dynamic objects from a <see cref="IEnumerable"/>.
15
24
/// </summary>
16
25
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
17
26
/// <returns>An array that contains the elements from the input sequence.</returns>
18
27
#if NET35
19
28
public static object [ ] ToDynamicArray ( [ NotNull ] this IEnumerable source )
29
+ {
30
+ Check . NotNull ( source , nameof ( source ) ) ;
31
+ return CastToArray < object > ( source ) ;
32
+ }
20
33
#else
21
34
public static dynamic [ ] ToDynamicArray ( [ NotNull ] this IEnumerable source )
22
- #endif
23
35
{
24
36
Check . NotNull ( source , nameof ( source ) ) ;
25
- return CastToArray < object > ( source ) ;
37
+ return CastToArray < dynamic > ( source ) ;
26
38
}
39
+ #endif
27
40
28
41
/// <summary>
29
42
/// Creates an array of dynamic objects from a <see cref="IEnumerable"/>.
30
43
/// </summary>
31
- /// <typeparam name="T"></typeparam>
44
+ /// <typeparam name="T">The generic type. </typeparam>
32
45
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
33
46
/// <returns>An Array{T} that contains the elements from the input sequence.</returns>
34
47
public static T [ ] ToDynamicArray < T > ( [ NotNull ] this IEnumerable source )
@@ -37,6 +50,29 @@ public static T[] ToDynamicArray<T>([NotNull] this IEnumerable source)
37
50
return CastToArray < T > ( source ) ;
38
51
}
39
52
53
+ /// <summary>
54
+ /// Creates an array of dynamic objects from a <see cref="IEnumerable"/>.
55
+ /// </summary>
56
+ /// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
57
+ /// <param name="type">A <see cref="Type"/> cast to.</param>
58
+ /// <returns>An Array that contains the elements from the input sequence.</returns>
59
+ #if NET35
60
+ public static object [ ] ToDynamicArray ( [ NotNull ] this IEnumerable source , [ NotNull ] Type type )
61
+ #else
62
+ public static dynamic [ ] ToDynamicArray ( [ NotNull ] this IEnumerable source , [ NotNull ] Type type )
63
+ #endif
64
+ {
65
+ Check . NotNull ( source , nameof ( source ) ) ;
66
+ Check . NotNull ( type , nameof ( type ) ) ;
67
+
68
+ object result = ToDynamicArrayGenericMethod . MakeGenericMethod ( type ) . Invoke ( source , new object [ ] { source } ) ;
69
+ #if NET35
70
+ return ( object [ ] ) result ;
71
+ #else
72
+ return ( dynamic [ ] ) result ;
73
+ #endif
74
+ }
75
+
40
76
/// <summary>
41
77
/// Creates a list of dynamic objects from a <see cref="IEnumerable"/>.
42
78
/// </summary>
@@ -59,7 +95,25 @@ public static List<dynamic> ToDynamicList([NotNull] this IEnumerable source)
59
95
/// <summary>
60
96
/// Creates a list of dynamic objects from a <see cref="IEnumerable"/>.
61
97
/// </summary>
62
- /// <typeparam name="T"></typeparam>
98
+ /// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
99
+ /// <param name="type">A <see cref="Type"/> cast to.</param>
100
+ /// <returns>A List that contains the elements from the input sequence.</returns>
101
+ #if NET35
102
+ public static List < object > ToDynamicList ( [ NotNull ] this IEnumerable source , [ NotNull ] Type type )
103
+ #else
104
+ public static List < dynamic > ToDynamicList ( [ NotNull ] this IEnumerable source , [ NotNull ] Type type )
105
+ #endif
106
+ {
107
+ Check . NotNull ( source , nameof ( source ) ) ;
108
+ Check . NotNull ( type , nameof ( type ) ) ;
109
+
110
+ return ToDynamicArray ( source , type ) . ToList ( ) ;
111
+ }
112
+
113
+ /// <summary>
114
+ /// Creates a list of dynamic objects from a <see cref="IEnumerable"/>.
115
+ /// </summary>
116
+ /// <typeparam name="T">Generic Type</typeparam>
63
117
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
64
118
/// <returns>A List{T} that contains the elements from the input sequence.</returns>
65
119
public static List < T > ToDynamicList < T > ( [ NotNull ] this IEnumerable source )
0 commit comments