首页 文章

动态构建具有未知类型的LINQ表达式

提问于
浏览
2

我想知道是否可以使用此表达式构建器类动态生成lambda表达式,其中在编译时不知道要过滤的类型 .

我有一个构造表达式的方法,

public static Expression<Func<T, bool>> GetExpression<T>(IList<QueryFilter> filters)

和一个QueryFilter对象,

public class QueryFilter 
{
    public string PropertyName { get; set; }       
    public ExpressionType OpType { get; set; } 
    public object Value { get; set; }
}

你可以在哪里生成一个新的表达式,使用传入的QueryFilters过滤对象T.我想开发一个类型未知的方法,即 .

public static Expression<Func<T,bool>> GetExpression(IList<QueryFilter> filters, Type type)

所以我可以将类型作为参数传递给System.Reflection,而不必在代码中指定它 . 例如,沿着这些线,

public static Expression NewExpression(IList<QueryFilter> filters, Type T)
    {
        return GetExpression<Type>(filters); 
    }

如果可以使用这种语法,Type替换泛型'T'?由于我认为我不能在<>括号内指定运行时动态类型,是否有另一种方法,也许使用

Func<object,bool>

代替?

2 回答

相关问题