dynamicexpresso / DynamicExpresso

C# expressions interpreter
http://dynamic-expresso.azurewebsites.net/
MIT License
1.91k stars 364 forks source link

Is assignment to DynamicObject supported? #272

Closed 1andy11 closed 1 year ago

1andy11 commented 1 year ago

Hello,

if I do the following statement interpreter.Parse("dynamic_object.some_property = x", new DynamicExpresso.Parameter[] { new Parameter("x", typeof(object)) }); I get the exception DynamicExpresso.Exceptions.ParseException: 'Expression must be writable (at index 30).'

Is it possible to achieve somehow such assignment?

Regards, Andrej.

davideicardi commented 1 year ago

How have you defined dynamic_object.some_property?

1andy11 commented 1 year ago

I have derived class from DynamicObject. Reading is working.

public class MyDynamicObject : DynamicObject
{
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
        ...
        }
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
        ...
        }
}
metoule commented 1 year ago

The following C# code doesn't compile:

using System;
using System.Dynamic;

public class Program
{
  public class MyDynamicObject : DynamicObject
  {
  }

  public static void Main()
  {
    var dynamic_object = new MyDynamicObject();
    dynamic_object.some_property = new Object();
  }
}

Compilation error (line 13, col 19): 'Program.MyDynamicObject' does not contain a definition for 'some_property' and no extension method 'some_property' accepting a first argument of type 'Program.MyDynamicObject' could be found (are you missing a using directive or an assembly reference?)

Do you have a C# example that actually compiles?