From ed4823b3d0ec88d7bbeb63bf37b6edfbc15a9127 Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 18 Nov 2026 13:50:09 +0500 Subject: [PATCH] binding: fix the get accessor check in the indexer lookup The indexer lookup was meant to skip properties without a get accessor, but its check 'mi?.GetParameters().Length != 0' is true when the accessor is null, because null is not equal to 0. As a result, indexing a value whose type declared a write-only property failed with "Object reference not set to an instance of an object", even when the type had a readable indexer. A type with only a write-only indexer failed the same way instead of reporting that indexing cannot be applied to it. Compare the parameter count with '> 0', which is false for a missing accessor, both when collecting the indexer candidates and when listing them for an ambiguous match. --- .../ExpressionBuilder.cs | 6 ++-- .../ExpressionParserTest.cs | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index 972ddfc..0518444 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -215,8 +215,8 @@ Expression ProcessIndex() foreach (var pi in self.Type.GetProperties()) { var mi = pi.GetMethod; - if (mi?.GetParameters().Length != 0) - methods.Add(mi!); + if (mi?.GetParameters().Length > 0) + methods.Add(mi); } if (methods.Count == 0) @@ -225,7 +225,7 @@ Expression ProcessIndex() var method = (MethodInfo?)MethodResolver.ResolveMethod(methods.ToArray(), parameterTypes); if (method is null) { - var list = self.Type.GetProperties().Where(p => p.GetMethod?.GetParameters().Length != 0); + var list = self.Type.GetProperties().Where(p => p.GetMethod?.GetParameters().Length > 0); Error.AmbiguousMatch(list); } diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 4df0300..0a7bb41 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -217,6 +217,21 @@ public void Parse_ByteArgumentForCharParameter_ReturnsError() Assert.That(result.Success, Is.False); } + [Test] + public void Evaluate_TypeWithWriteOnlyProperties_UsesReadableIndexer() => + Assert.That(ExpressionParser.Evaluate("Mixed[1]", new IndexerContext()), Is.EqualTo("item 1")); + + [Test] + public void Parse_TypeWithWriteOnlyIndexer_ReturnsNonIndexingError() + { + var parameter = Expression.Parameter(typeof(IndexerContext), "c"); + var result = ExpressionParser.Parse("WriteOnly[1]", new ContextAwareBinder(parameter)); + + Assert.That(result.Success, Is.False); + Assert.That(result.ErrorMessage, Is.EqualTo( + $"Cannot apply indexing with [] to an expression of type '{typeof(WriteOnlyIndexer)}'.")); + } + [TestCaseSource(nameof(ErrorsSource))] public void ParseErrorExpression(string expression, string expected) { @@ -426,6 +441,26 @@ public class OverloadContext public static string ExpandedBothDefaults(int a, int b = 0, int c = 0, params int[] rest) => "(int, int = 0, int = 0, params int[])"; } + public class IndexerContext + { + public MixedAccessors Mixed { get; } = new(); + public WriteOnlyIndexer WriteOnly { get; } = new(); + } + + public class MixedAccessors + { + private string? _name; + + public string this[int index] => $"item {index}"; + public string this[string key] { set { } } + public string Name { set => _name = value; } + } + + public class WriteOnlyIndexer + { + public string this[int index] { set { } } + } + public class ParamArrayContext { public string[] Words { get; } = ["a", "b"];