Skip to content

Commit a708a28

Browse files
authored
Remove option for 'UseDynamicObjectClassForAnonymousTypes' (#353)
* fix * remove config setting
1 parent 849952f commit a708a28

File tree

4 files changed

+22
-38
lines changed

4 files changed

+22
-38
lines changed

src/System.Linq.Dynamic.Core/DynamicClass.net35.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#if NET35
2+
using System.Collections.Generic;
3+
24
namespace System.Linq.Dynamic.Core
35
{
46
/// <summary>

src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

+19-31
Original file line numberDiff line numberDiff line change
@@ -1400,40 +1400,30 @@ private Expression CreateNewExpression(List<DynamicProperty> properties, List<Ex
14001400

14011401
if (type == null)
14021402
{
1403-
#if !UAP10_0
1404-
if (_parsingConfig != null && _parsingConfig.UseDynamicObjectClassForAnonymousTypes)
1405-
{
1406-
#endif
1407-
type = typeof(DynamicClass);
1408-
Type typeForKeyValuePair = typeof(KeyValuePair<string, object>);
1409-
#if NET35 || NET40
1410-
ConstructorInfo constructorForKeyValuePair = typeForKeyValuePair.GetConstructors().First();
1411-
#else
1412-
ConstructorInfo constructorForKeyValuePair = typeForKeyValuePair.GetTypeInfo().DeclaredConstructors.First();
1413-
#endif
1414-
var arrayIndexParams = new List<Expression>();
1415-
for (int i = 0; i < expressions.Count; i++)
1416-
{
1417-
// Just convert the expression always to an object expression.
1418-
UnaryExpression boxingExpression = Expression.Convert(expressions[i], typeof(object));
1419-
NewExpression parameter = Expression.New(constructorForKeyValuePair, (Expression)Expression.Constant(properties[i].Name), boxingExpression);
1403+
#if UAP10_0
1404+
type = typeof(DynamicClass);
1405+
Type typeForKeyValuePair = typeof(KeyValuePair<string, object>);
14201406

1421-
arrayIndexParams.Add(parameter);
1422-
}
1407+
ConstructorInfo constructorForKeyValuePair = typeForKeyValuePair.GetTypeInfo().DeclaredConstructors.First();
14231408

1424-
// Create an expression tree that represents creating and initializing a one-dimensional array of type KeyValuePair<string, object>.
1425-
NewArrayExpression newArrayExpression = Expression.NewArrayInit(typeof(KeyValuePair<string, object>), arrayIndexParams);
1409+
var arrayIndexParams = new List<Expression>();
1410+
for (int i = 0; i < expressions.Count; i++)
1411+
{
1412+
// Just convert the expression always to an object expression.
1413+
UnaryExpression boxingExpression = Expression.Convert(expressions[i], typeof(object));
1414+
NewExpression parameter = Expression.New(constructorForKeyValuePair, (Expression)Expression.Constant(properties[i].Name), boxingExpression);
14261415

1427-
// Get the "public DynamicClass(KeyValuePair<string, object>[] propertylist)" constructor
1428-
#if NET35 || NET40
1429-
ConstructorInfo constructor = type.GetConstructors().First();
1430-
#else
1431-
ConstructorInfo constructor = type.GetTypeInfo().DeclaredConstructors.First();
1432-
#endif
1433-
return Expression.New(constructor, newArrayExpression);
1434-
#if !UAP10_0
1416+
arrayIndexParams.Add(parameter);
14351417
}
14361418

1419+
// Create an expression tree that represents creating and initializing a one-dimensional array of type KeyValuePair<string, object>.
1420+
NewArrayExpression newArrayExpression = Expression.NewArrayInit(typeof(KeyValuePair<string, object>), arrayIndexParams);
1421+
1422+
// Get the "public DynamicClass(KeyValuePair<string, object>[] propertylist)" constructor
1423+
ConstructorInfo constructor = type.GetTypeInfo().DeclaredConstructors.First();
1424+
1425+
return Expression.New(constructor, newArrayExpression);
1426+
#else
14371427
type = DynamicClassFactory.CreateType(properties, _createParameterCtor);
14381428
#endif
14391429
}
@@ -1454,7 +1444,6 @@ private Expression CreateNewExpression(List<DynamicProperty> properties, List<Ex
14541444
for (int i = 0; i < propertyTypes.Length; i++)
14551445
{
14561446
Type propertyType = propertyTypes[i];
1457-
// Type expressionType = expressions[i].Type;
14581447

14591448
// Promote from Type to Nullable Type if needed
14601449
expressionsPromoted.Add(_parsingConfig.ExpressionPromoter.Promote(expressions[i], propertyType, true, true));
@@ -1468,7 +1457,6 @@ private Expression CreateNewExpression(List<DynamicProperty> properties, List<Ex
14681457
{
14691458
PropertyInfo property = type.GetProperty(properties[i].Name);
14701459
Type propertyType = property.PropertyType;
1471-
// Type expressionType = expressions[i].Type;
14721460

14731461
// Promote from Type to Nullable Type if needed
14741462
bindings[i] = Expression.Bind(property, _parsingConfig.ExpressionPromoter.Promote(expressions[i], propertyType, true, true));

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

-7
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,6 @@ public IQueryableAnalyzer QueryableAnalyzer
9595
/// </summary>
9696
public bool AreContextKeywordsEnabled { get; set; } = true;
9797

98-
/// <summary>
99-
/// Gets or sets a value indicating whether to use dynamic object class for anonymous types.
100-
///
101-
/// Default value is false.
102-
/// </summary>
103-
public bool UseDynamicObjectClassForAnonymousTypes { get; set; } = false;
104-
10598
/// <summary>
10699
/// Gets or sets a value indicating whether the EntityFramework version supports evaluating GroupBy at database level.
107100
/// See https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.1#linq-groupby-translation

test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq.Dynamic.Core.Tests.Helpers.Models;
66
using System.Linq.Expressions;
77
using System.Reflection;
8+
using FluentAssertions;
89
using Xunit;
910
using User = System.Linq.Dynamic.Core.Tests.Helpers.Models.User;
1011

0 commit comments

Comments
 (0)