Use objects in Flee expression (with properties & functions)

Oct 23, 2013 at 9:46 PM
Hello,
I have a simple question, but don't know if the answer is simple.

Consider the following expression:
IDynamicExpression e = ruleContext.CompileDynamic("x + 2*x.value + x.multiply(2)");

x will be defined as an object, with properties and functions.

I would like to:
  • be able to type only "x" -> then I would like to use x.value
  • be able to use properties : x.value, or functions x.multiply(10)
My question
  • In this case, the call to "x" does not work, as x is an object.
  • Is there a way to make it work with a trick in the code, or should I make a REGEX query that would modify all "x" to "x.value" (may be tricky as x can have many names) ?
I send the code in a 2nd post
Oct 23, 2013 at 9:46 PM
Edited Oct 23, 2013 at 9:47 PM
complete code:


ruleContext = new ExpressionContext();
ruleContext.Imports.AddType(typeof(FleeVariable));
ruleContext.Variables.ResolveVariableType += new EventHandler<ResolveVariableTypeEventArgs>(Variables_ResolveVariableType);

IDynamicExpression e = ruleContext.CompileDynamic("x + 2*x.value + x.multiply(2)");

calcRule.ruleContext.Variables["x"] = new FleeVariable(10);

int test = e.evaluate()


private void ResolveVariableType(object sender, ResolveVariableTypeEventArgs e)
{
e.VariableType = typeof (FleeVariable);
}

public class FleeVariable
{
public int value;
public FleeVariable(int val)
{
    this.value = val;
}

public double multiply(double a)
{
    return a * this.value;
}
}
Oct 24, 2013 at 12:47 AM
Edited Oct 24, 2013 at 12:47 AM
you could just overload operator on your class