Skip to content

Feature Request: Ability to do assignment with DynamicExpresso #24

@iiaiiappa

Description

@iiaiiappa

First of all, thank you for writing DynamicExpresso. It worked as claimed.

I am trying to do assignment. Right now I did the assignment using function call, which is less than ideal.

public class ObjectA { public object Value; }
public class ObjectB { public object Value; }

public void Test1()
{
    var interpreter = new Interpreter();
    var objA = new ObjectA();
    var objB = new ObjectB();
    var data = new Dictionary<string, dynamic>() {
        {"a",objA},
        {"b",objB},
    };
    var paramsList2 = pData.Select(r => new Parameter(r.Key, r.Value)).ToArray();
    interpreter.Eval("a.Value = b.Value", paramsList2);

    Debug.WriteLine(objA.Value);
    Debug.WriteLine(objB.Value);
}

public void Test2()
{
    var interpreter = new Interpreter();
    var objA = new ObjectA();
    var objB = new ObjectB();
    var data = new Dictionary<string, dynamic>() {
        {"a",objA},
        {"b",objB},
    };
    //suppose I set all the SetFunction() here.
    ....
    //Attempting to make Func and Action class working in string expression
    interpreter.Reference(typeof(Func<bool>));
    interpreter.Reference(typeof(Action));
    var paramsList2 = pData.Select(r => new Parameter(r.Key, r.Value)).ToArray();

    //The following won't work because => operator not supported
    interpreter.Eval("new Func<bool>(() => { a.Value = b.Value; return true; })", paramsList2);
    //The following won't work because it returns nothing, and => operator not supported
    interpreter.Eval("new Action(() => { a.Value = b.Value; })", paramsList2);
    //Works, but limited my possibility and flexibility
    interpreter.Eval("SetVal(a,b)", paramsList2);

    Debug.WriteLine(objA.Value);
    Debug.WriteLine(objB.Value);
}
public bool SetVal(dynamic pa, dynamic pb)
{
    pa.Value = pb.Value;
    return true;
}

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions