Skip to content

Commit 7d1e23e

Browse files
committed
Added new extension methods for ToDynamicArray and ToDynamicList with a Type. (Solved issue #48)
1 parent 62e8e35 commit 7d1e23e

File tree

2 files changed

+164
-4
lines changed

2 files changed

+164
-4
lines changed

src/System.Linq.Dynamic.Core/DynamicEnumerableExtensions.cs

+58-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq.Dynamic.Core.Validation;
5+
using System.Reflection;
56

67
namespace System.Linq.Dynamic.Core
78
{
@@ -10,25 +11,37 @@ namespace System.Linq.Dynamic.Core
1011
/// </summary>
1112
public static class DynamicEnumerableExtensions
1213
{
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+
1322
/// <summary>
1423
/// Creates an array of dynamic objects from a <see cref="IEnumerable"/>.
1524
/// </summary>
1625
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
1726
/// <returns>An array that contains the elements from the input sequence.</returns>
1827
#if NET35
1928
public static object[] ToDynamicArray([NotNull] this IEnumerable source)
29+
{
30+
Check.NotNull(source, nameof(source));
31+
return CastToArray<object>(source);
32+
}
2033
#else
2134
public static dynamic[] ToDynamicArray([NotNull] this IEnumerable source)
22-
#endif
2335
{
2436
Check.NotNull(source, nameof(source));
25-
return CastToArray<object>(source);
37+
return CastToArray<dynamic>(source);
2638
}
39+
#endif
2740

2841
/// <summary>
2942
/// Creates an array of dynamic objects from a <see cref="IEnumerable"/>.
3043
/// </summary>
31-
/// <typeparam name="T"></typeparam>
44+
/// <typeparam name="T">The generic type.</typeparam>
3245
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
3346
/// <returns>An Array{T} that contains the elements from the input sequence.</returns>
3447
public static T[] ToDynamicArray<T>([NotNull] this IEnumerable source)
@@ -37,6 +50,29 @@ public static T[] ToDynamicArray<T>([NotNull] this IEnumerable source)
3750
return CastToArray<T>(source);
3851
}
3952

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+
4076
/// <summary>
4177
/// Creates a list of dynamic objects from a <see cref="IEnumerable"/>.
4278
/// </summary>
@@ -59,7 +95,25 @@ public static List<dynamic> ToDynamicList([NotNull] this IEnumerable source)
5995
/// <summary>
6096
/// Creates a list of dynamic objects from a <see cref="IEnumerable"/>.
6197
/// </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>
63117
/// <param name="source">A <see cref="IEnumerable"/> to create an array from.</param>
64118
/// <returns>A List{T} that contains the elements from the input sequence.</returns>
65119
public static List<T> ToDynamicList<T>([NotNull] this IEnumerable source)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
2+
using Xunit;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
6+
namespace System.Linq.Dynamic.Core.Tests
7+
{
8+
public partial class EntitiesTests
9+
{
10+
[Fact]
11+
public void Entities_DynamicEnumerableExtensions_ToDynamicArray()
12+
{
13+
//Arrange
14+
var list = new List<SimpleValuesModel>();
15+
list.Add(new SimpleValuesModel { IntValue = 1 });
16+
list.Add(new SimpleValuesModel { IntValue = 2 });
17+
18+
//Act
19+
var expected = list.ToArray() as object[];
20+
var result = DynamicEnumerableExtensions.ToDynamicArray(list);
21+
22+
//Assert
23+
Assert.Equal(expected, result);
24+
}
25+
26+
[Fact]
27+
public void Entities_DynamicEnumerableExtensions_ToDynamicArray_Type1()
28+
{
29+
//Arrange
30+
var list = new List<SimpleValuesModel>();
31+
list.Add(new SimpleValuesModel { IntValue = 1 });
32+
list.Add(new SimpleValuesModel { IntValue = 2 });
33+
34+
//Act
35+
var expected = list.ToArray();
36+
var result = DynamicEnumerableExtensions.ToDynamicArray<SimpleValuesModel>(list);
37+
38+
//Assert
39+
Assert.Equal(expected, result);
40+
}
41+
42+
[Fact]
43+
public void Entities_DynamicEnumerableExtensions_ToDynamicArray_Type2()
44+
{
45+
//Arrange
46+
var list = new List<SimpleValuesModel>();
47+
list.Add(new SimpleValuesModel { IntValue = 1 });
48+
list.Add(new SimpleValuesModel { IntValue = 2 });
49+
50+
//Act
51+
var expected = list.ToArray();
52+
var result = DynamicEnumerableExtensions.ToDynamicArray(list, typeof(SimpleValuesModel));
53+
54+
//Assert
55+
Assert.Equal(expected, result);
56+
}
57+
58+
[Fact]
59+
public void Entities_DynamicEnumerableExtensions_ToDynamicList()
60+
{
61+
//Arrange
62+
var list = new List<SimpleValuesModel>();
63+
list.Add(new SimpleValuesModel { IntValue = 1 });
64+
list.Add(new SimpleValuesModel { IntValue = 2 });
65+
66+
//Act
67+
var expected = list.Select(sv => (object)sv);
68+
var result = DynamicEnumerableExtensions.ToDynamicList(list);
69+
70+
//Assert
71+
Assert.Equal(expected, result);
72+
}
73+
74+
[Fact]
75+
public void Entities_DynamicEnumerableExtensions_ToDynamicList_Type1()
76+
{
77+
//Arrange
78+
var list = new List<SimpleValuesModel>();
79+
list.Add(new SimpleValuesModel { IntValue = 1 });
80+
list.Add(new SimpleValuesModel { IntValue = 2 });
81+
82+
//Act
83+
var expected = list;
84+
var result = DynamicEnumerableExtensions.ToDynamicList<SimpleValuesModel>(list);
85+
86+
//Assert
87+
Assert.Equal(expected, result);
88+
}
89+
90+
[Fact]
91+
public void Entities_DynamicEnumerableExtensions_ToDynamicList_Type2()
92+
{
93+
//Arrange
94+
var list = new List<SimpleValuesModel>();
95+
list.Add(new SimpleValuesModel { IntValue = 1 });
96+
list.Add(new SimpleValuesModel { IntValue = 2 });
97+
98+
//Act
99+
var expected = list;
100+
var result = DynamicEnumerableExtensions.ToDynamicList(list, typeof(SimpleValuesModel));
101+
102+
//Assert
103+
Assert.Equal(expected, result);
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)