ParameterExpression name = Expression.Parameter(typeof(string), "name"),
value = Expression.Parameter(typeof(string), "value");
// build in reverse
Expression body = Expression.Constant(false);
body = Expression.Condition(
Expression.AndAlso(
Expression.Equal(name, Expression.Constant("GAS")),
Expression.Equal(value, Expression.Constant("G"))
), Expression.Constant(true), body);
body = Expression.Condition(
Expression.Equal(name, Expression.Constant("MV")),
Expression.Constant(true), body);
Expression<Func<string, string, bool>> featureEnabledExpTree =
Expression.Lambda<Func<string, string, bool>>(body, name, value);
// test in isolation
var featureEnabledFunc = featureEnabledExpTree.Compile();
bool isMatch1 = featureEnabledFunc("MV", "")
&& featureEnabledFunc("GAS", "G") || !featureEnabledFunc("GAS", "F");
然后,如果您需要second部分也作为表达式树:
//I want the equal of the following statement in C# Linq Expression
Expression<Func<bool>> test =
Expression.Lambda<Func<bool>>(
Expression.OrElse(
Expression.AndAlso(
Expression.Invoke(featureEnabledExpTree,
Expression.Constant("MV"),
Expression.Constant("")
),
Expression.Invoke(featureEnabledExpTree,
Expression.Constant("GAS"),
Expression.Constant("G")
)
),
Expression.Not(
Expression.Invoke(featureEnabledExpTree,
Expression.Constant("GAS"),
Expression.Constant("F")
)
)
)
);
bool isMatch = test.Compile()();