From 8e168189604cb221e08ee038b3a5424ccbaaa860 Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 18 Nov 2026 17:25:48 +0500 Subject: [PATCH] parser: use TContext for the context parameter in Evaluate Evaluate created the context parameter with the runtime type of the context but compiled a delegate that takes TContext. When the context was an instance of a derived or implementing type, such as Evaluate("Value", impl), it failed with "ParameterExpression of type 'Impl' cannot be used for delegate parameter of type 'IFace'". Create the parameter with TContext, so the expression can access the members of TContext. Members declared only in the runtime type of the context are not available, as for a variable of type TContext in C#. --- .../ExpressionParser.cs | 6 +++- .../ExpressionParserTest.cs | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionParser.cs b/src/Ramstack.ExpressionParser/ExpressionParser.cs index 7e25115..8cbdb40 100644 --- a/src/Ramstack.ExpressionParser/ExpressionParser.cs +++ b/src/Ramstack.ExpressionParser/ExpressionParser.cs @@ -156,9 +156,13 @@ public static ParseResult Parse(ReadOnlySpan source, Expressio /// /// The evaluated result of type . /// + /// + /// The expression can access the members of , not of the runtime type + /// of . Members declared only in a derived or implementing type are not available. + /// public static TResult? Evaluate(ReadOnlySpan source, TContext context) where TContext : notnull { - var c = Expression.Parameter(context.GetType(), "context"); + var c = Expression.Parameter(typeof(TContext), "context"); var r = Parse(source, new ContextAwareBinder(c)); if (!r.Success) diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 55b58bf..0247661 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -127,6 +127,24 @@ public void ContextTest() public void EvaluateExpression(string expression, object result) => Assert.That(ExpressionParser.Evaluate(expression), Is.EqualTo(result)); + [Test] + public void Evaluate_ContextOfDerivedType_BindsMembersOfDeclaredType() + { + var context = new ValueContext(); + + Assert.That(ExpressionParser.Evaluate("Value", context), Is.EqualTo(1)); + Assert.That(ExpressionParser.Evaluate("ToString()", context), Is.EqualTo(context.ToString())); + } + + [Test] + public void Evaluate_MemberOfRuntimeContextTypeOnly_Throws() + { + var exception = Assert.Throws(() => + ExpressionParser.Evaluate("Extra", new ValueContext())); + + Assert.That(exception!.Message, Is.EqualTo("Cannot resolve symbol 'Extra'.")); + } + [TestCase("-1u", -1L)] [TestCase("-4294967295u", -4294967295L)] [TestCase("-3000000000", -3000000000L)] @@ -446,6 +464,17 @@ public class UserInfo public string? LastName { get; set; } } + public interface IValueContext + { + int Value { get; } + } + + public class ValueContext : IValueContext + { + public int Value { get; } = 1; + public int Extra { get; } = 2; + } + public class LogicalNotContext { public bool Flag { get; } = true;