本文整理汇总了C#中ExpressionContext.CompileGeneric方法的典型用法代码示例。如果您正苦于以下问题:C# ExpressionContext.CompileGeneric方法的具体用法?C# ExpressionContext.CompileGeneric怎么用?C# ExpressionContext.CompileGeneric使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionContext
的用法示例。
在下文中一共展示了ExpressionContext.CompileGeneric方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
示例2: btnSearch_Click
//.........这里部分代码省略.........
}
}
catch (Exception ex)
{
MessageBox.Show("Error encountered while trying to evaluate custom expression parameter.\n\n" + ex.Message);
return;
}
}
else if (Tools.IsNumeric(parts[k]))
{
string part = parts[k++];
foreach (int id in includedIDs)
{
if (!ignoredIDs.Contains(id))
{
expByPlayer[id] += part;
}
if (!ignoredPlIDs.Contains(id))
{
plExpByPlayer[id] += part;
}
}
i += part.Length - 1;
}
}
foreach (int id in includedIDs)
{
curPSR = psrListSea.Single(psr => psr.ID == id);
curPSRPl = psrListPl.Single(psr => psr.ID == id);
if (!ignoredIDs.Contains(id))
{
try
{
IGenericExpression<double> compiled = context.CompileGeneric<double>(expByPlayer[id]);
curPSR.Custom.Add(compiled.Evaluate());
}
catch (Exception ex)
{
message = string.Format("Expression {0}:\n{1}\nevaluated to\n{2}\n\nError: {3}", (j + 1), item,
expByPlayer[id], ex.Message);
curPSR.Custom.Add(double.NaN);
}
}
else
{
curPSR.Custom.Add(double.NaN);
}
if (!ignoredPlIDs.Contains(id))
{
try
{
IGenericExpression<double> compiled = context.CompileGeneric<double>(plExpByPlayer[id]);
curPSRPl.Custom.Add(compiled.Evaluate());
}
catch (Exception ex)
{
message = string.Format("Expression {0}:\n{1}\nevaluated to\n{2}\n\nError: {3}", (j + 1), item,
plExpByPlayer[id], ex.Message);
curPSRPl.Custom.Add(double.NaN);
}
}
else
{
curPSRPl.Custom.Add(double.NaN);
}
}
示例3: filter
/// <summary>
/// Used to filter the PlayerStatsRow results based on any user-specified metric stats criteria.
/// </summary>
/// <param name="o">The o.</param>
/// <returns></returns>
private bool filter(object o)
{
var psr = (PlayerStatsRow) o;
var ps = new PlayerStats(psr);
bool keep = true;
var context = new ExpressionContext();
IGenericExpression<bool> ige;
if (!String.IsNullOrWhiteSpace(txtYearsProVal.Text))
{
ige = context.CompileGeneric<bool>(psr.YearsPro.ToString() + cmbYearsProOp.SelectedItem + txtYearsProVal.Text);
if (ige.Evaluate() == false)
{
return false;
}
}
if (!String.IsNullOrWhiteSpace(txtYOBVal.Text))
{
context = new ExpressionContext();
ige = context.CompileGeneric<bool>(psr.YearOfBirth.ToString() + cmbYOBOp.SelectedItem + txtYOBVal.Text);
if (ige.Evaluate() == false)
{
return false;
}
}
if (!String.IsNullOrWhiteSpace(txtHeightVal.Text))
{
double metricHeight = MainWindow.IsImperial
? PlayerStatsRow.ConvertImperialHeightToMetric(txtHeightVal.Text)
: Convert.ToDouble(txtHeightVal.Text);
context = new ExpressionContext();
ige = context.CompileGeneric<bool>(psr.Height.ToString() + cmbHeightOp.SelectedItem + metricHeight.ToString());
if (ige.Evaluate() == false)
{
return false;
}
}
if (!String.IsNullOrWhiteSpace(txtWeightVal.Text))
{
double imperialWeight = MainWindow.IsImperial
? Convert.ToDouble(txtWeightVal.Text)
: PlayerStatsRow.ConvertMetricWeightToImperial(txtWeightVal.Text);
context = new ExpressionContext();
ige = context.CompileGeneric<bool>(psr.Weight.ToString() + cmbWeightOp.SelectedItem + imperialWeight.ToString());
if (ige.Evaluate() == false)
{
return false;
}
}
if (!String.IsNullOrWhiteSpace(txtContractYLeftVal.Text))
{
context = new ExpressionContext();
ige =
context.CompileGeneric<bool>((chkExcludeOption.IsChecked.GetValueOrDefault()
? psr.ContractYearsMinusOption.ToString()
: psr.ContractYears.ToString()) + cmbContractYLeftOp.SelectedItem +
txtContractYLeftVal.Text);
if (ige.Evaluate() == false)
{
return false;
}
}
foreach (string contractYear in lstContract.Items.Cast<string>())
{
string[] parts = contractYear.Split(' ');
ige =
context.CompileGeneric<bool>(psr.GetType().GetProperty("ContractY" + parts[1]).GetValue(psr, null) + parts[2] +
parts[3]);
keep = ige.Evaluate();
if (!keep)
{
return keep;
}
}
IEnumerable<string> totalsFilters = lstTotals.Items.Cast<string>();
Parallel.ForEach(totalsFilters, (item, loopState) =>
{
string[] parts = item.Split(' ');
parts[0] = parts[0].Replace("3P", "TP");
parts[0] = parts[0].Replace("TO", "TOS");
context = new ExpressionContext();
ige = context.CompileGeneric<bool>(psr.GetType().GetProperty(parts[0]).GetValue(psr, null) + parts[1] + parts[2]);
keep = ige.Evaluate();
if (!keep)
{
loopState.Stop();
}
});
if (!keep)
{
return keep;
}
//.........这里部分代码省略.........
示例4: btnSearch_Click
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
_playersToHighlight.Clear();
_teamsToHighlight.Clear();
var bsToCalculate = new List<BoxScoreEntry>();
var notBsToCalculate = new List<BoxScoreEntry>();
foreach (BoxScoreEntry bse in MainWindow.BSHist)
{
bool keep = true;
foreach (var filter in _filters)
{
if (filter.Key.SelectionType == SelectionType.Team)
{
if (!_teamsToHighlight.Contains(filter.Key.ID))
{
_teamsToHighlight.Add(filter.Key.ID);
}
//string teamName = MainWindow.TeamOrder.Single(pair => pair.Value == filter.Key.ID).Key;
if (bse.BS.Team1ID != filter.Key.ID && bse.BS.Team2ID != filter.Key.ID)
{
keep = false;
break;
}
string p = bse.BS.Team1ID == filter.Key.ID ? "1" : "2";
string oppP = p == "1" ? "2" : "1";
foreach (Filter option in filter.Value)
{
string parameter;
if (!option.Parameter1.StartsWith("Opp"))
{
switch (option.Parameter1)
{
case "PF":
parameter = "PTS" + p;
break;
case "PA":
parameter = "PTS" + oppP;
break;
default:
parameter = option.Parameter1 + p;
break;
}
}
else
{
parameter = option.Parameter1.Substring(3) + oppP;
}
parameter = parameter.Replace("3P", "TP");
parameter = parameter.Replace("TO", "TOS");
string parameter2;
if (!option.Parameter2.StartsWith("Opp"))
{
switch (option.Parameter2)
{
case "PF":
parameter2 = "PTS" + p;
break;
case "PA":
parameter2 = "PTS" + oppP;
break;
default:
parameter2 = option.Parameter2 + p;
break;
}
}
else
{
parameter2 = option.Parameter2.Substring(3) + oppP;
}
parameter2 = parameter2.Replace("3P", "TP");
parameter2 = parameter2.Replace("TO", "TOS");
var context = new ExpressionContext();
IGenericExpression<bool> ige;
if (String.IsNullOrWhiteSpace(parameter2))
{
if (!parameter.Contains("%"))
{
ige =
context.CompileGeneric<bool>(string.Format("{0} {1} {2}",
bse.BS.GetType()
.GetProperty(parameter)
.GetValue(bse.BS, null), option.Operator,
option.Value));
}
else
{
string par1 = parameter.Replace("%", "M");
string par2 = parameter.Replace("%", "A");
ige =
context.CompileGeneric<bool>(
string.Format("(Cast({0}, double) / Cast({1}, double)) {2} Cast({3}, double)",
bse.BS.GetType().GetProperty(par1).GetValue(bse.BS, null),
bse.BS.GetType().GetProperty(par2).GetValue(bse.BS, null), option.Operator,
option.Value));
}
}
//.........这里部分代码省略.........
示例5: btnSearch_Click
//.........这里部分代码省略.........
var parameter2 = "";
if (!String.IsNullOrWhiteSpace(option.Parameter2))
{
if (!option.Parameter2.StartsWith("Opp"))
{
switch (option.Parameter2)
{
case "PF":
parameter2 = "PTS" + p;
break;
case "PA":
parameter2 = "PTS" + oppP;
break;
default:
parameter2 = option.Parameter2 + p;
break;
}
}
else
{
parameter2 = option.Parameter2.Substring(3) + oppP;
}
parameter2 = parameter2.Replace("3P", "TP");
parameter2 = parameter2.Replace("TO", "TOS");
}
var context = new ExpressionContext();
IGenericExpression<bool> ige;
if (String.IsNullOrWhiteSpace(parameter2))
{
if (!parameter.Contains("%"))
{
ige =
context.CompileGeneric<bool>(
string.Format(
"{0} {1} {2}",
bse.BS.GetType().GetProperty(parameter).GetValue(bse.BS, null),
option.Operator,
option.Value));
}
else
{
var par1 = parameter.Replace("%", "M");
var par2 = parameter.Replace("%", "A");
ige =
context.CompileGeneric<bool>(
string.Format(
"(Cast({0}, double) / Cast({1}, double)) {2} Cast({3}, double)",
bse.BS.GetType().GetProperty(par1).GetValue(bse.BS, null),
bse.BS.GetType().GetProperty(par2).GetValue(bse.BS, null),
option.Operator,
option.Value));
}
}
else
{
if (!parameter.Contains("%"))
{
if (!parameter2.Contains("%"))
{
ige =
context.CompileGeneric<bool>(
string.Format(
"{0} {1} {2}", getValue(bse, parameter), option.Operator, getValue(bse, parameter2)));
}
else
示例6: filter
/// <summary>Used to filter the PlayerStatsRow results based on any user-specified metric stats criteria.</summary>
/// <param name="o">The o.</param>
/// <returns></returns>
private bool filter(object o)
{
var psr = (PlayerStatsRow) o;
var ps = new PlayerStats(psr);
var keep = true;
var context = new ExpressionContext();
IGenericExpression<bool> ige;
if (!String.IsNullOrWhiteSpace(_yearsProVal))
{
ige = context.CompileGeneric<bool>(psr.YearsPro.ToString() + _yearsProOp + _yearsProVal);
if (ige.Evaluate() == false)
{
return false;
}
}
if (!String.IsNullOrWhiteSpace(_yobVal))
{
context = new ExpressionContext();
ige = context.CompileGeneric<bool>(psr.YearOfBirth.ToString() + _yobOp + _yobVal);
if (ige.Evaluate() == false)
{
return false;
}
}
if (!String.IsNullOrWhiteSpace(_heightVal))
{
var metricHeight = MainWindow.IsImperial
? PlayerStatsRow.ConvertImperialHeightToMetric(_heightVal)
: Convert.ToDouble(_heightVal);
context = new ExpressionContext();
ige = context.CompileGeneric<bool>(psr.Height.ToString() + _heightOp + metricHeight.ToString());
if (ige.Evaluate() == false)
{
return false;
}
}
if (!String.IsNullOrWhiteSpace(_weightVal))
{
var imperialWeight = MainWindow.IsImperial
? Convert.ToDouble(_weightVal)
: PlayerStatsRow.ConvertMetricWeightToImperial(_weightVal);
context = new ExpressionContext();
ige = context.CompileGeneric<bool>(psr.Weight.ToString() + _weightOp + imperialWeight.ToString());
if (ige.Evaluate() == false)
{
return false;
}
}
if (!String.IsNullOrWhiteSpace(_contractYearsLeftVal))
{
context = new ExpressionContext();
ige =
context.CompileGeneric<bool>(
(chkExcludeOption.IsChecked.GetValueOrDefault()
? psr.ContractYearsMinusOption.ToString()
: psr.ContractYears.ToString()) + _contractYearsLeftOp + _contractYearsLeftVal);
if (ige.Evaluate() == false)
{
return false;
}
}
foreach (var contractYear in _contractYears)
{
var parts = contractYear.Split(' ');
ige =
context.CompileGeneric<bool>(
psr.GetType().GetProperty("ContractY" + parts[1]).GetValue(psr, null) + parts[2] + parts[3]);
keep = ige.Evaluate();
if (!keep)
{
return false;
}
}
var stopped = false;
Parallel.ForEach(
_totalsFilters,
(item, loopState) =>
{
var parts = item.Split(' ');
parts[0] = parts[0].Replace("3P", "TP");
parts[0] = parts[0].Replace("TO", "TOS");
context = new ExpressionContext();
ige =
context.CompileGeneric<bool>(psr.GetType().GetProperty(parts[0]).GetValue(psr, null) + parts[1] + parts[2]);
keep = ige.Evaluate();
if (!keep)
{
stopped = true;
loopState.Stop();
}
});
if (!keep || stopped)
//.........这里部分代码省略.........
示例7: 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);
}
示例8: compileExpression
private void compileExpression()
{
ExpressionContext context = new ExpressionContext();
// Allows use of all public static Math functions:
//context.Imports.AddType(typeof(Math));
// Load values into Flee context.
foreach (KeyValuePair<string, long> pair in formulaIDToIntegerValue)
{
context.Variables[pair.Key] = pair.Value;
}
foreach (KeyValuePair<string, double> pair in formulaIDToFloatingPointValue)
{
context.Variables[pair.Key] = pair.Value;
}
compiledExpression = context.CompileGeneric<double>(cleanFormula(ScoringFormula));
}
示例9: ReadWidgets
public Widgets ReadWidgets(List<Filter> filters)
{
Widgets widgets = _repository;
string linqExpression = filters.ToLinqExpression<Widget>("o");
if (linqExpression != String.Empty)
{
ExpressionContext context = new ExpressionContext();
context.Variables.DefineVariable("o", typeof(Widget));
for (int i = 0; i < widgets.Count; i++)
{
context.Variables["o"] = widgets[i];
var expression = context.CompileGeneric<bool>(linqExpression);
try
{
if (!expression.Evaluate())
{
widgets.RemoveAt(i--);
}
}
catch { } //If Expression fails to eval for item ignore item.
}
}
return widgets;
}