using System;
using System.Linq.Expressions;
class DynamicPredicate
{
public static Expression<Func<T, T, bool>> Generate<T>(string op)
{
ParameterExpression x = Expression.Parameter(typeof(T), "x");
ParameterExpression y = Expression.Parameter(typeof(T), "y");
return Expression.Lambda<Func<T, T, bool>>
(
(op.Equals(">")) ? Expression.GreaterThan(x, y) :
(op.Equals("<")) ? Expression.LessThan(x, y) :
(op.Equals(">=")) ? Expression.GreaterThanOrEqual(x, y) :
(op.Equals("<=")) ? Expression.LessThanOrEqual(x, y) :
(op.Equals("!=")) ? Expression.NotEqual(x, y) :
Expression.Equal(x, y),
x,
y
);
}
}
static void Main()
{
string op = ">=";
var integerPredicate = DynamicPredicate.Generate<int>(op).Compile();
var floatPredicate = DynamicPredicate.Generate<float>(op).Compile();
int iA = 12, iB = 4;
Console.WriteLine("{0} {1} {2} : {3}",
iA, op, iB, integerPredicate(iA, iB));
float fA = 867.0f, fB = 867.0f;
Console.WriteLine("{0} {1} {2} : {3}",
fA, op, fB, floatPredicate(fA, fB));
Console.WriteLine("{0} {1} {2} : {3}",
fA, ">", fB, DynamicPredicate.Generate<float>(">").Compile()(fA, fB));
Console.ReadLine();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有