Skip to content

Commit 90776ef

Browse files
committed
opt
1 parent 3030208 commit 90776ef

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,12 @@ public bool ExpressionQualifiesForNullPropagation(Expression? expression)
361361
public Expression GenerateDefaultExpression(Type type)
362362
{
363363
#if NET35
364-
return Expression.Constant(Activator.CreateInstance(type));
364+
if (type.IsValueType)
365+
{
366+
return Expression.Constant(Activator.CreateInstance(type), type);
367+
}
368+
369+
return Expression.Constant(null, type);
365370
#else
366371
return Expression.Default(type);
367372
#endif
@@ -390,38 +395,22 @@ public bool TryConvertTypes(ref Expression left, ref Expression right)
390395
{
391396
left = Expression.Condition(
392397
Expression.Equal(left, Expression.Constant(null, typeof(object))),
393-
GenerateDefault(right.Type),
398+
GenerateDefaultExpression(right.Type),
394399
Expression.Convert(left, right.Type)
395400
);
396401
}
397402
else if (right.Type == typeof(object))
398403
{
399404
right = Expression.Condition(
400405
Expression.Equal(right, Expression.Constant(null, typeof(object))),
401-
GenerateDefault(left.Type),
406+
GenerateDefaultExpression(left.Type),
402407
Expression.Convert(right, left.Type)
403408
);
404409
}
405410

406411
return true;
407412
}
408413

409-
public Expression GenerateDefault(Type type)
410-
{
411-
#if NET35
412-
// .NET 3.5 doesn't have Expression.Default
413-
if (type.IsValueType)
414-
{
415-
return Expression.Constant(Activator.CreateInstance(type), type);
416-
}
417-
418-
return Expression.Constant(null, type);
419-
#else
420-
return Expression.Default(type);
421-
#endif
422-
}
423-
424-
425414
private Expression? GetMemberExpression(Expression? expression)
426415
{
427416
if (ExpressionQualifiesForNullPropagation(expression))

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,4 @@ internal interface IExpressionHelper
5353
/// If the types are different (and not null), try to convert the object type to other type.
5454
/// </summary>
5555
bool TryConvertTypes(ref Expression left, ref Expression right);
56-
57-
Expression GenerateDefault(Type type);
5856
}

0 commit comments

Comments
 (0)