本文整理汇总了C#中ExpressionContext.CompileDynamic方法的典型用法代码示例。如果您正苦于以下问题:C# ExpressionContext.CompileDynamic方法的具体用法?C# ExpressionContext.CompileDynamic怎么用?C# ExpressionContext.CompileDynamic使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionContext
的用法示例。
在下文中一共展示了ExpressionContext.CompileDynamic方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compile
public void Compile(ExpressionContext context)
{
// TODO: verify wheather the expression is a value (no need to compile)
bool isValue = false;
if (!isValue)
eDynamic = context.CompileDynamic(AssignmentExpression);
}
示例2: Test
private static void Test(IQuestion qs)
{
Console.WriteLine(qs.Question);
var q = qs.Question;
q = q.Replace("pluss", "+").
Replace("minus", "-").
Replace("ganger", "*").
Replace("delt på", "/").
Replace("Hva er", "");
Console.WriteLine(q);
var context = new ExpressionContext();
context.Imports.AddType(typeof (Math));
var eDynamic = context.CompileDynamic(q);
var res = eDynamic.Evaluate();
Assert.IsTrue(qs.Run("" + res));
}
示例3: GetReferencedVariables
public static string[] GetReferencedVariables(string expression, string[] variables)
{
if (String.IsNullOrWhiteSpace(expression))
return null;
if ((variables == null) ||(variables.Length < 1))
return null;
ExpressionContext context = new ExpressionContext();
// Use string.format
context.Imports.AddType(typeof(string));
//context.Imports.AddType(typeof(CustomFunctions));
for (int i=0;i<variables.Length;i++)
context.Variables[variables[i]] = 1.0;
IDynamicExpression e = context.CompileDynamic(expression);
return e.Info.GetReferencedVariables();
}
示例4: Main
public static void Main()
{
ExpressionContext context = new ExpressionContext();
context.Options.EmitToAssembly = true;
//context.ParserOptions.RequireDigitsBeforeDecimalPoint = True
//context.ParserOptions.DecimalSeparator = ","c
//context.ParserOptions.RecreateParser()
//context.Options.ResultType = GetType(Decimal)
context.Variables["a"] = 2;
context.Variables["b"] = 4;
IDynamicExpression e1 = context.CompileDynamic("If (19 in (13,28,33,48,71,73,101,102,103,104,23,23,234,34,345,345,45,34,34,4555,445,20),1,0)");
var res1 = e1.Evaluate();
var e = context.CompileGeneric<bool>("b > a");
object result = e.Evaluate();
Console.ReadLine();
}
示例5: Evaluate
//public List<double> Evaluate(string expression, DataTable dt)
public DataTable Evaluate(string expression, DataTable dt)
{
List<double> lstCalcValues = new List<double>();
DataTable dtCalcValues = new DataTable();
dtCalcValues.Columns.Add("ID", typeof(string));
dtCalcValues.Columns.Add("CalcValue", typeof(double));
MyTable = dt;
ExpressionContext context = new ExpressionContext();
// Use string.format
context.Imports.AddType(typeof(string));
// Use on demand variables to provide the values for the columns
context.Variables.ResolveVariableType += new EventHandler<ResolveVariableTypeEventArgs>(Variables_ResolveVariableType);
context.Variables.ResolveVariableValue += new EventHandler<ResolveVariableValueEventArgs>(Variables_ResolveVariableValue);
// Create the expression; Flee will now query for the types of ItemName, Price, and Tax
IDynamicExpression e = context.CompileDynamic(expression);
Console.WriteLine("Computing value of '{0}' for all rows", e.Text);
DataRow dr = null;
for (int i = 0; i < dt.Rows.Count; i++)
{
MyCurrentRow = dt.Rows[i];
// Evaluate the expression; Flee will query for the values of the columns
double dlbResult = (double) e.Evaluate();
lstCalcValues.Add(dlbResult);
dr = dtCalcValues.NewRow();
dr[0] = MyCurrentRow[0] as string;
dr[1] = dlbResult;
dtCalcValues.Rows.Add(dr);
Console.WriteLine("Row {0} = {1}", i, dlbResult);
}
return dtCalcValues;
}
示例6: CreateExpressionEvaluator
private Func<double, double> CreateExpressionEvaluator(string expression)
{
ExpressionContext context = new ExpressionContext();
context.Options.EmitToAssembly = false;
context.Imports.AddType(typeof(Math));
context.Variables["x"] = 0.0d;
IDynamicExpression fx = null;
try
{
fx = context.CompileDynamic(expression);
}
catch (ExpressionCompileException)
{
return null;
}
Func<Double, Double> expressionEvaluator = (Double i) =>
{
context.Variables["x"] = i;
return (Double)fx.Evaluate();
};
return expressionEvaluator;
}
示例7: C
public C(int times)
{
var r = new Random();
var context = new ExpressionContext();
context.Imports.AddType(typeof (Math));
var c = 0;
var qs = Enumerable.Range(0, times).ToList().Select(i =>
{
var type = _types.Keys.ToList()[r.Next(_types.Count)];
c++;
var v = "a" + c;
context.Variables[v] = r.Next(-2000, 10000);
return string.Format("{0} {1}", v, type);
});
Q = string.Join(" ", qs.ToArray()).Trim();
Q = Q.Substring(0, Q.Length - 1);
var eDynamic = context.CompileDynamic(Q);
Res = (int) eDynamic.Evaluate();
Console.WriteLine(Q + " = " + Res);
_types.ToList().ForEach(type => Q = Q.Replace(type.Key, type.Value));
context.Variables.Keys.ToList().ForEach(key => Q = Q.Replace(key, "" + context.Variables[key]));
}
示例8: ExpressionEvaluationTest
public void ExpressionEvaluationTest()
{
ExpressionContext context = new ExpressionContext();
context.Variables.Add("rand", new Random());
IDynamicExpression e = context.CompileDynamic("rand.nextDouble() + 100");
double result = (double)e.Evaluate();
//no LINQ
//var doc = new XDocument(new XElement("test",
// new XElement("val", "result1"),
// new XElement("val", "result2"),
// new XElement("val", "result3")
// ));
//context.Variables.Add("doc", doc);
//e = context.CompileDynamic("doc.Elements().First().Value");
//var r = e.Evaluate();
//no Dynamic
//dynamic expando = new ExpandoObject();
//expando.test = "Passed";
//context.Variables.Add("expando", expando);
//e = context.CompileDynamic("expando.test");
//var r = e.Evaluate();
}
示例9: CreateExpression
private IDynamicExpression CreateExpression(string expression, ExpressionContext context)
{
return context.CompileDynamic(expression);
}
示例10: Compile
public void Compile(ExpressionContext context)
{
if (_expression != string.Empty && _expression != "true" && _expression != "false")
eDynamic = context.CompileDynamic(_expression);
}
示例11: TestNoStateHeldInContext
public void TestNoStateHeldInContext()
{
ExpressionContext context = new ExpressionContext();
IGenericExpression<int> e1 = context.CompileGeneric<int>("300");
// The result type of the cloned context should be set to integer
Assert.IsTrue(object.ReferenceEquals(e1.Context.Options.ResultType, typeof(Int32)));
// The original context should not be affected
Assert.IsNull(context.Options.ResultType);
// This should compile
IDynamicExpression e2 = context.CompileDynamic("\"abc\"");
Assert.IsTrue(object.ReferenceEquals(e2.Context.Options.ResultType, typeof(string)));
// The original context should not be affected
Assert.IsNull(context.Options.ResultType);
}