如何在泛型类型上更新表达式值

时间:2019-06-08 10:54:35

标签: c# generics lambda expression

我正在传递表达式

InsertAsync(caseCommentDto, e => e.CaseNumber == caseCommentDto.CaseNumber && e.CommentSequenceNumber == caseCommentDto.CommentSequenceNumber)

转到我的网关方法

public async Task<BaseResult> InsertAsync(TEntity entity, Expression<Func<TEntity, bool>> expression)
    {
        //Get SequenceID for example CaseNumber

        var sequenceIdField = typeof(SequenceIds).GetField(typeof(TEntity).Name).GetValue(null);
        var sequenceField = typeof(Sequences).GetField(typeof(TEntity).Name).GetValue(null);
        var entityType = typeof(TEntity).Name;
        var sequenceIdValue = typeof(TEntity).GetProperty(sequenceIdField.ToString())?.GetValue(entity);
        var sequenceNumber = _amhUnitOfWork.GetNextSequenceAsync(sequenceIdValue.ToString()).Result.Payload;
        typeof(TEntity).GetProperty(sequenceField.ToString())?.SetValue(entity, sequenceNumber);

        //var parma = expression.(sequenceNumber.ToString()).
        //expression = { expression=>}
        //someList.Select(x => { x.SomeProp = "foo"; return x; })
        //expression.Update();SetProperty();e
        //expression.Parameters.
        //SetProperty(expression,sequenceField.ToString(), sequenceIdValue);
        typeof(Expression<Func<TEntity, bool>>).GetField(sequenceField.ToString())?.SetValue(expression, sequenceNumber);

        return await _amhUnitOfWork.InsertAsync(entity, expression).ConfigureAwait(false);
    }

这里的所有内容都是动态确定的。我已经能够使用代码设置实体的值。

typeof(TEntity).GetProperty(sequenceField.ToString())?.SetValue(entity, sequenceNumber);

如何对谓词执行相同的操作。

这似乎可行,但是不确定,我无法验证。我如何验证它是否有效。

typeof(Expression<Func<TEntity, bool>>).GetField(sequenceField.ToString())?.SetValue(expression, sequenceNumber);

编辑:确认上述操作无效。

0 个答案:

没有答案