c# – 如何修改Expression>的类型参数?

前端之家收集整理的这篇文章主要介绍了c# – 如何修改Expression>的类型参数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下实例:
Expression<Func<IrequiredDate,bool>>

我希望将其转换为以下实例,因此它可用于在Entity Framework中运行查询

Expression<Func<TModel,bool>>

这将允许我对任何实现IrequiredDate的Model使用通用过滤查询,例如:

// In some repository function:
var query = DbContext.Set<Order>()
     .FilterByDateRange(DateTime.Today,DateTime.Today);

var query = DbContext.Set<Note>()
     .FilterByDateRange(DateTime.Today,DateTime.Today);

var query = DbContext.Set<Complaint>()
     .FilterByDateRange(DateTime.Today,DateTime.Today);


// The general purpose function,can filter for any model implementing IrequiredDate
public static IQueryable<TModel> FilterByDate<TModel>(IQueryable<TModel> query,DateTime startDate,DateTime endDate) where TModel : IrequiredDate
{
    // This will NOT WORK,as E/F won't accept an expression of type IrequiredDate,even though TModel implements IrequiredDate
    // Expression<Func<IrequiredDate,bool>> dateRangeFilter = x => x.Date >= startDate && x.Date <= endDate;
    // query = query.Where(dateRangeFilter);

    // This also WON'T WORK,x.Date is compiled into the expression as a member of IrequiredDate instead of TModel,so E/F knocks it back for the same reason:
    // Expression<Func<TModel,bool>> dateRangeFilter = x => x.Date >= startDate && x.Date <= endDate;
    // query = query.Where(dateRangeFilter);

    // All you need is lov.... uh... something like this:
    Expression<Func<IrequiredDate,bool>> dateRangeFilter = x => x.Date >= startDate && x.Date <= endDate;
    Expression<Func<TModel,bool>> dateRangeFilterForType = ConvertExpressionType<IrequiredDate,TModel>(dateRangeFilter); // Must convert the expression from one type to another
    query = query.Where(dateRangeFilterForType) // Ahhhh. this will work.

    return query;
}

public static ConvertExpressionType<TInterface,TModel>(Expression<Func<TInterface,bool>> expression)
where TModel : TInterface // It must implement the interface,since we're about to translate them
{
    Expression<Func<TModel,bool>> newExpression = null;

    // TODO: How to convert the contents of expression into newExpression,modifying the
    // generic type parameter along the way??

    return newExpression;
}

我知道他们是不同的类型,不能演员.但是我想知道是否有办法创建一个新的表达式< Func< TModel,bool>>,然后根据Expression< Func< IrequiredDate,bool>>的内容重建它.提供,在过程中将任何类型的引用从IrequiredDate切换到TModel.

可以这样做吗?

解决方法

所以实际进行映射的方法并不那么难,但遗憾的是,我没有一种很好的方法可以看出它的推广.这是一个采用Func< T1,TResult>的方法.并将其映射到一个委托,其中参数是比T1更多的派生:
public static Expression<Func<NewParam,TResult>> Foo<NewParam,OldParam,TResult>(
    Expression<Func<OldParam,TResult>> expression)
    where NewParam : OldParam
{
    var param = Expression.Parameter(typeof(NewParam));
    return Expression.Lambda<Func<NewParam,TResult>>(
        expression.Body.Replace(expression.Parameters[0],param),param);
}

这使用Replace方法将一个表达式的所有实例替换为另一个表达式.定义是:

internal class ReplaceVisitor : ExpressionVisitor
{
    private readonly Expression from,to;
    public ReplaceVisitor(Expression from,Expression to)
    {
        this.from = from;
        this.to = to;
    }
    public override Expression Visit(Expression node)
    {
        return node == from ? to : base.Visit(node);
    }
}

public static Expression Replace(this Expression expression,Expression searchEx,Expression replaceEx)
{
    return new ReplaceVisitor(searchEx,replaceEx).Visit(expression);
}

现在我们可以使用这个方法(应该给出一个更好的名字),如下所示:

Expression<Func<object,bool>> oldExpression = whatever;
Expression<Func<string,bool>> newExpression =
    Foo<string,object,bool>(oldExpression);

当然,由于Func实际上是关于其参数的协变,我们可以确定对此方法的任何调用都会生成不会添加运行时故障点的表达式.

您可以为Func< T1,T2,TResult>等轻松制作此版本,依此类推,直到16种不同类型的Func,如果您愿意,只需为每个类型创建一个参数表达式,并替换所有旧的新的.这很乏味,但只是遵循这种模式.鉴于旧的和新的参数类型都需要有一个泛型参数,并且没有办法推断出参数,那就太麻烦了.

原文链接:https://www.f2er.com/csharp/92153.html

猜你在找的C#相关文章