From 416edcb2ea3e48bf67370afe6d0afb65aa1e3d53 Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 18 Nov 2026 16:07:39 +0500 Subject: [PATCH] parser: fix the assertion for method calls on delegates Calling a method on a member of a delegate type, such as 'func.Invoke(1)' or 'func.ToString()', failed an assertion in Debug builds, because the assertion expected a method to be called only on an instance that is not a delegate. An application using a Debug build of the library terminated with "Assertion failed". Release builds were not affected. Assert only that an instance without a method is a delegate to invoke. --- src/Ramstack.ExpressionParser/ExpressionBuilder.cs | 7 +++++-- .../ExpressionParserTest.cs | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index cc3b8ca..ef8bfcb 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -260,8 +260,11 @@ Expression Invoke(Expression? instance, MethodInfo? method) if (instance is not null) { - Debug.Assert(method is null && typeof(Delegate).IsAssignableFrom(instance.Type) - || method is not null && !typeof(Delegate).IsAssignableFrom(instance.Type)); + // + // Without a method, the instance is a delegate to invoke. A method can also be called + // on a delegate, as in 'func.Invoke(1)' or 'func.ToString()'. + // + Debug.Assert(method is not null || typeof(Delegate).IsAssignableFrom(instance.Type)); if (method is null) { diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 6000cd7..cece885 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -163,6 +163,12 @@ public void Evaluate_ArrayForParamArray_PassesArray(string expression, object ex public void Evaluate_ElementsForParamArray_CreatesArray(string expression, object expected) => Assert.That(ExpressionParser.Evaluate(expression, new ParamArrayContext()), Is.EqualTo(expected)); + [TestCase("Doubler(2)", 4)] + [TestCase("Doubler.Invoke(2)", 4)] + [TestCase("Doubler.ToString()", "System.Func`2[System.Int32,System.Int32]")] + public void Evaluate_DelegateMember_CallsDelegateAndItsMethods(string expression, object expected) => + Assert.That(ExpressionParser.Evaluate(expression, new DelegateContext()), Is.EqualTo(expected)); + [TestCase("Defaults()", "1 Monday True null")] [TestCase("""Defaults(5, DayOfWeek.Friday, Guid.NewGuid(), "a")""", "5 Friday False a")] [TestCase("Mixed(1)", "1 2 0")] @@ -491,6 +497,11 @@ public class ParamArrayContext public bool IsWords(params string[] values) => ReferenceEquals(values, Words); } + public class DelegateContext + { + public Func Doubler { get; } = x => x * 2; + } + public class OptionalContext { public static string Defaults(int number = 1, DayOfWeek? day = DayOfWeek.Monday, Guid id = default, string? text = null) =>