From af653cc1a8564a5cc5bf5486eec49ac415177f50 Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 18 Nov 2026 16:14:56 +0500 Subject: [PATCH] parser: invoke static delegate members accessed through a type Invoking a static member of a delegate type through the name of its type, such as 'Formulas.Square(2)' for a registered type with a static Func Square, failed with "Cannot access a member on a null reference", because the code expected the member to always have an instance. Access the member without an instance, since a member bound through a type is static. --- .../ExpressionBuilder.cs | 6 +----- .../ExpressionParserTest.cs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index ef8bfcb..634790b 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -301,12 +301,8 @@ Expression ProcessMemberAccess(Expr.MemberAccess e) Error.NonInvocableMember(m); // - // This shouldn't happen, but we'll crash if it does + // A member accessed through a type is static, so it is accessed without an instance. // - if (expression is null) - throw new InvalidOperationException( - "Cannot access a member on a null reference."); - expression = Expression.MakeMemberAccess(expression, memberInfo); return Invoke(expression, null); } diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index cece885..e3daec2 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -169,6 +169,20 @@ public void Evaluate_ElementsForParamArray_CreatesArray(string expression, objec public void Evaluate_DelegateMember_CallsDelegateAndItsMethods(string expression, object expected) => Assert.That(ExpressionParser.Evaluate(expression, new DelegateContext()), Is.EqualTo(expected)); + [TestCase("StaticDelegates.Tripler(2)")] + [TestCase("StaticDelegates.Tripler.Invoke(2)")] + public void Evaluate_StaticDelegateMemberOfType_CallsDelegate(string expression) + { + var binder = new DefaultBinder(); + binder.RegisterType(typeof(StaticDelegates)); + + var result = ExpressionParser.Parse(expression, binder); + Assert.That(result.Success, Is.True, result.ErrorMessage); + + var value = Expression.Lambda>(result.Value!).Compile()(); + Assert.That(value, Is.EqualTo(6)); + } + [TestCase("Defaults()", "1 Monday True null")] [TestCase("""Defaults(5, DayOfWeek.Friday, Guid.NewGuid(), "a")""", "5 Friday False a")] [TestCase("Mixed(1)", "1 2 0")] @@ -502,6 +516,11 @@ public class DelegateContext public Func Doubler { get; } = x => x * 2; } + public static class StaticDelegates + { + public static Func Tripler { get; } = x => x * 3; + } + public class OptionalContext { public static string Defaults(int number = 1, DayOfWeek? day = DayOfWeek.Monday, Guid id = default, string? text = null) =>