Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
opt
  • Loading branch information
StefH committed Nov 2, 2025
commit 90776ef667d6ca2f3f694356b81b400069a184ef
27 changes: 8 additions & 19 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,12 @@ public bool ExpressionQualifiesForNullPropagation(Expression? expression)
public Expression GenerateDefaultExpression(Type type)
{
#if NET35
return Expression.Constant(Activator.CreateInstance(type));
if (type.IsValueType)
{
return Expression.Constant(Activator.CreateInstance(type), type);
}

return Expression.Constant(null, type);
#else
return Expression.Default(type);
#endif
Expand Down Expand Up @@ -390,38 +395,22 @@ public bool TryConvertTypes(ref Expression left, ref Expression right)
{
left = Expression.Condition(
Expression.Equal(left, Expression.Constant(null, typeof(object))),
GenerateDefault(right.Type),
GenerateDefaultExpression(right.Type),
Expression.Convert(left, right.Type)
);
}
else if (right.Type == typeof(object))
{
right = Expression.Condition(
Expression.Equal(right, Expression.Constant(null, typeof(object))),
GenerateDefault(left.Type),
GenerateDefaultExpression(left.Type),
Expression.Convert(right, left.Type)
);
}

return true;
}

public Expression GenerateDefault(Type type)
{
#if NET35
// .NET 3.5 doesn't have Expression.Default
if (type.IsValueType)
{
return Expression.Constant(Activator.CreateInstance(type), type);
}

return Expression.Constant(null, type);
#else
return Expression.Default(type);
#endif
}


private Expression? GetMemberExpression(Expression? expression)
{
if (ExpressionQualifiesForNullPropagation(expression))
Expand Down
2 changes: 0 additions & 2 deletions src/System.Linq.Dynamic.Core/Parser/IExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,4 @@ internal interface IExpressionHelper
/// If the types are different (and not null), try to convert the object type to other type.
/// </summary>
bool TryConvertTypes(ref Expression left, ref Expression right);

Expression GenerateDefault(Type type);
}
Loading