Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1518,10 +1518,12 @@ Expression ParseTypeAccess(Type type)
{
Expression[] args = shorthand ? new[] { ParseStringLiteral() } : ParseArgumentList();

// If only 1 argument, and the arg is ConstantExpression, just return the ConstantExpression
Comment thread
StefH marked this conversation as resolved.
if (args.Length == 1 && args[0] is ConstantExpression)
// If only 1 argument, and the arg is ConstantExpression, return the conversion
// If only 1 argument, and the arg is null, return the conversion (Can't use constructor)
if (args.Length == 1
&& (args[0] == null || args[0] is ConstantExpression))
{
return args[0];
return GenerateConversion(args[0], type, errorPos);
}

// If only 1 argument, and if the type is a ValueType and argType is also a ValueType, just Convert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public void Parse_ParseAndOperator(string expression, string result)
}

[Theory]
[InlineData("string(null)", null)]
[InlineData("string(\"\")", "")]
[InlineData("string(\"a\")", "a")]
public void Parse_CastStringShouldReturnConstantExpression(string expression, object result)
[InlineData("int(42)", 42)]
public void Parse_CastStringIntShouldReturnConstantExpression(string expression, object result)
{
// Arrange
ParameterExpression[] parameters = { ParameterExpressionHelper.CreateParameterExpression(typeof(bool), "x") };
Expand All @@ -88,20 +88,27 @@ public void Parse_CastStringShouldReturnConstantExpression(string expression, ob
}

[Theory]
[InlineData("int?(null)", null)]
[InlineData("int?(5)", 5)]
[InlineData("int(42)", 42)]
public void Parse_CastIntShouldReturnConstantExpression(string expression, object result)
#if NET452
[InlineData("int?(5)", typeof(int?), "Convert(5)")]
[InlineData("int?(null)", typeof(int?), "Convert(null)")]
[InlineData("string(null)", typeof(string), "Convert(null)")]
#else
[InlineData("int?(5)", typeof(int?), "Convert(5, Nullable`1)")]
[InlineData("int?(null)", typeof(int?), "Convert(null, Nullable`1)")]
[InlineData("string(null)", typeof(string), "Convert(null, String)")]
#endif
public void Parse_NullableShouldReturnNullable(string expression, object resultType, object result)
{
// Arrange
ParameterExpression[] parameters = { ParameterExpressionHelper.CreateParameterExpression(typeof(bool), "x") };
var sut = new ExpressionParser(parameters, expression, null, null);

// Act
var constantExpression = (ConstantExpression)sut.Parse(null);
var unaryExpression = (UnaryExpression)sut.Parse(null);

// Assert
Check.That(constantExpression.Value).Equals(result);
Check.That(unaryExpression.Type).Equals(resultType);
Check.That(unaryExpression.ToString()).Equals(result);
}
}
}