Skip to content

DynamicObject w/ index access throws ParseException #127

Description

@marcOcram

Hello,

I tried to access a dynamic object by index and the library seems to be unable to parse the expression on .NET Core 3.1 and .NET 4.7.2.

using DynamicExpresso;
using System;
using System.Dynamic;
using System.Collections.Generic;

public class Program
{
	public class Globals {
		private readonly DynamicIndexAccess _values = new DynamicIndexAccess();
		
		public dynamic Values { get { return _values; } }
	}
	
	public class DynamicIndexAccess : DynamicObject {
		private readonly IReadOnlyDictionary<string, object> _values;

		public DynamicIndexAccess()
		{
			var values = new Dictionary<string, object>();
			values.Add("Hello", "Hello World!");
			_values = values;
		}

		public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
		{
			return _values.TryGetValue((string)indexes[0], out result);
		}

		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			return _values.TryGetValue(binder.Name, out result);
		}
	}
	
	public static void Main()
	{
		Globals globals = new Globals();
		Interpreter interpreter = new Interpreter()
			.SetVariable("Values", globals.Values);
		// runs
		Console.WriteLine(globals.Values.Hello);
		// runs
		Console.WriteLine(interpreter.Eval<string>("Values.Hello"));
		// runs
		Console.WriteLine(globals.Values["Hello"]);
		// fails
		Console.WriteLine(interpreter.Eval<string>("Values[\"Hello\"]"));
	}
}

Stacktrace

DynamicExpresso.Exceptions.ParseException: No applicable indexer exists in type 'DynamicIndexAccess' (at index 6).
   at DynamicExpresso.Parsing.Parser.ParseElementAccess(Expression expr)
   at DynamicExpresso.Parsing.Parser.ParsePrimary()
   at DynamicExpresso.Parsing.Parser.ParseUnary()
   at DynamicExpresso.Parsing.Parser.ParseMultiplicative()
   at DynamicExpresso.Parsing.Parser.ParseAdditive()
   at DynamicExpresso.Parsing.Parser.ParseTypeTesting()
   at DynamicExpresso.Parsing.Parser.ParseComparison()
   at DynamicExpresso.Parsing.Parser.ParseLogicalAnd()
   at DynamicExpresso.Parsing.Parser.ParseLogicalXor()
   at DynamicExpresso.Parsing.Parser.ParseLogicalOr()
   at DynamicExpresso.Parsing.Parser.ParseConditionalAnd()
   at DynamicExpresso.Parsing.Parser.ParseConditionalOr()
   at DynamicExpresso.Parsing.Parser.ParseConditional()
   at DynamicExpresso.Parsing.Parser.ParseAssignment()
   at DynamicExpresso.Parsing.Parser.ParseExpressionSegment()
   at DynamicExpresso.Parsing.Parser.ParseExpressionSegment(Type returnType)
   at DynamicExpresso.Parsing.Parser.Parse()
   at DynamicExpresso.Interpreter.ParseAsLambda(String expressionText, Type expressionType, Parameter[] parameters)
   at DynamicExpresso.Interpreter.Parse(String expressionText, Type expressionType, Parameter[] parameters)
   at DynamicExpresso.Interpreter.Eval(String expressionText, Type expressionType, Parameter[] parameters)
   at DynamicExpresso.Interpreter.Eval[T](String expressionText, Parameter[] parameters)
   at Program.Main()

Reproduction using the code above is available as fiddle.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions