Skip to content

Commit f42dfb6

Browse files
authored
Refactor method calls and add new tests (#851)
Modified `#if` directive in `NotNullWhenAttribute.cs` to include `NETSTANDARD2_0` and remove `NETSTANDARD1_3_OR_GREATER`. Updated `GenerateStaticMethodCall` in `ExpressionHelper.cs` to use `TryGetStaticMethod` instead of `GetStaticMethod`, throwing an `ArgumentException` if the method is not found. Replaced `GetStaticMethod` with `TryGetStaticMethod` in `ExpressionHelper.cs`. Added `MyView` class with nullable `Properties` dictionary to `ExpressionParserTests.cs`. Added `Parse_InvalidExpressionShouldThrowArgumentException` test to verify `ArgumentException` is thrown for invalid expressions.
1 parent 83010f8 commit f42dfb6

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

src/System.Linq.Dynamic.Core/Compatibility/CodeAnalysis/NotNullWhenAttribute.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// SOFTWARE.
2323
#endregion
2424

25-
#if NETSTANDARD1_3_OR_GREATER || NET35 || NET40 || NET45 || NET452 || NET46 || NETCOREAPP2_1 || UAP10_0
25+
#if NETSTANDARD1_3 || NETSTANDARD2_0 || NET35 || NET40 || NET45 || NET452 || NET46 || NETCOREAPP2_1 || UAP10_0
2626

2727
// ReSharper disable once CheckNamespace
2828
namespace System.Diagnostics.CodeAnalysis;

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,11 @@ private void TryConvertTypes(ref Expression left, ref Expression right)
477477

478478
private static Expression GenerateStaticMethodCall(string methodName, Expression left, Expression right)
479479
{
480-
var methodInfo = GetStaticMethod(methodName, left, right);
480+
if (!TryGetStaticMethod(methodName, left, right, out var methodInfo))
481+
{
482+
throw new ArgumentException($"Method '{methodName}' not found on type '{left.Type}' or '{right.Type}'");
483+
}
484+
481485
var parameters = methodInfo.GetParameters();
482486
var parameterTypeLeft = parameters[0].ParameterType;
483487
var parameterTypeRight = parameters[1].ParameterType;
@@ -495,15 +499,10 @@ private static Expression GenerateStaticMethodCall(string methodName, Expression
495499
return Expression.Call(null, methodInfo, [left, right]);
496500
}
497501

498-
private static MethodInfo GetStaticMethod(string methodName, Expression left, Expression right)
502+
private static bool TryGetStaticMethod(string methodName, Expression left, Expression right, [NotNullWhen(true)] out MethodInfo? methodInfo)
499503
{
500-
var methodInfo = left.Type.GetMethod(methodName, [left.Type, right.Type]);
501-
if (methodInfo == null)
502-
{
503-
methodInfo = right.Type.GetMethod(methodName, [left.Type, right.Type])!;
504-
}
505-
506-
return methodInfo;
504+
methodInfo = left.Type.GetMethod(methodName, [left.Type, right.Type]) ?? right.Type.GetMethod(methodName, [left.Type, right.Type]);
505+
return methodInfo != null;
507506
}
508507

509508
private static Expression? GetMethodCallExpression(MethodCallExpression methodCallExpression)

test/System.Linq.Dynamic.Core.Tests/Parser/ExpressionParserTests.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public enum ExampleFlags
2727
D = 8,
2828
};
2929

30+
public class MyView
31+
{
32+
public Dictionary<string, string>? Properties { get; set; }
33+
}
3034

3135
public ExpressionParserTests()
3236
{
@@ -421,8 +425,18 @@ public void Parse_StringConcat(string expression, string result)
421425

422426
// Act
423427
var parsedExpression = parser.Parse(typeof(string)).ToString();
424-
428+
425429
// Assert
426430
parsedExpression.Should().Be(result);
427431
}
432+
433+
[Fact]
434+
public void Parse_InvalidExpressionShouldThrowArgumentException()
435+
{
436+
// Arrange & Act
437+
Action act = () => DynamicExpressionParser.ParseLambda<MyView, bool>(ParsingConfig.Default, false, "Properties[\"foo\"] > 2", Array.Empty<object>());
438+
439+
// Assert
440+
act.Should().Throw<ArgumentException>().WithMessage("Method 'Compare' not found on type 'System.String' or 'System.Int32'");
441+
}
428442
}

0 commit comments

Comments
 (0)