public void Call_() { var dt = DateTime.Parse("1/2/2017 03:04:05"); var sss = dt.ToString(); ExpressionContext ctx = new ExpressionContext(); ctx.AddVariable(typeof(TestClass), "instance", new TestClass()); CompileContext compile = new CompileContext(); compile.AddVariable(typeof(TestClass), "instance"); Run(Resource1.call, compile, ctx); }
public void Func_Var_Nest_Func() { var varI = Variable <int>("i"); var funcExpr = Function("func_i", Block(new ParameterExpression[] { varI }, Assign(varI, Constant(2)), Return(Call(Function(null, Return(varI), typeof(int)), typeof(int)))), typeof(int)); var compile = new CompileContext(); //using (compile.BeginScope()) //{ compile.AddVariable <int>("i"); var tmp = compile.Compile(Call(funcExpr, typeof(int))); var ctx = new ExpressionContext(); ctx.AddVariable <int>("i", 1); var result = tmp(ctx); Assert.AreEqual(2, result); //} }
public static object Eval(Dictionary <string, object> variables, Expression expression, ExpressionContext ctx = null) { if (ctx == null) { ctx = new ExpressionContext(); } var compile = new CompileContext(); InvocationDelegate eval; if (variables != null) { foreach (var item in variables) { compile.AddVariable(item.Value.GetType(), item.Key); ctx.AddVariable(item.Key, item.Value); } } eval = compile.Compile(expression); return(eval(ctx)); }
public void Type_Convert() { MyValue my = new MyValue("a"); string myStr = my; Assert.AreEqual("a", myStr); my = myStr; Assert.AreEqual("a", my.Value); MyValue2 my2 = new MyValue2("a2"); myStr = my2; //my2 = myStr;//can't convert Assert.AreEqual("a2", myStr); MyValue3 my3 = myStr; myStr = my3; Func <object, Type, object> convert = (value, toType) => { Type fromType = value.GetType(); InvocationDelegate eval; var compile = new CompileContext(); compile.OverrideOperator(typeof(MyValue)); compile.AddVariable(fromType, "a"); using (compile.BeginScope()) { var expr = Convert(Variable(fromType, "a"), toType); eval = compile.Compile(expr); } ExpressionContext ctx = new ExpressionContext(); ctx.AddVariable(fromType, "a"); ctx.SetVariable("a", value); return(eval(ctx)); }; object result; result = convert(new MyValue("Hello World"), typeof(string)); Assert.AreEqual("Hello World", result); result = convert("Hello World", typeof(MyValue)); Assert.AreEqual("Hello World", ((MyValue)result).Value); result = convert(new MyValue2("Hello World"), typeof(string)); Assert.AreEqual("Hello World", result); try { result = convert("Hello World", typeof(MyValue2)); Assert.AreEqual("Hello World", ((MyValue2)result).Value); Assert.Fail(); } catch (OperatorNotImplementedException ex) { } result = convert(new MyValue3("Hello World"), typeof(string)); Assert.AreEqual("Hello World", result); result = convert("Hello World", typeof(MyValue3)); Assert.AreEqual("Hello World", ((MyValue3)result).Value); }