日期:2014-05-20 浏览次数:21314 次
int num = 100;
Expression conditionExpr = Expression.Condition(
Expression.Constant(num > 10),
Expression.Constant("num is greater than 10"),
Expression.Constant("num is smaller than 10")
);
Console.WriteLine(conditionExpr.ToString());
Console.WriteLine(
Expression.Lambda<Func<string>>(conditionExpr).Compile()());
bool test = true;
Expression ifThenElseExpr = Expression.IfThenElse(
Expression.Constant(test),
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("The condition is true.")
),
Expression.Call(
null,
typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
Expression.Constant("The condition is false.")
)
);
Expression.Lambda<Action>(ifThenElseExpr).Compile()();