无法将`Expression <func <t1,t2 =“”>&gt;`转换为`Expression <func <object,object =“”>&gt;`

时间:2017-03-22 12:40:43

标签: c# generics expression

更新:此问题被标记为重复,但我了解代码的问题,但我没有解决方案。是否可以通过仅更改方法体而不是方法签名来使代码工作?

在尝试构建如下所示的类时,我想绕着ExpressionFunc进行包裹:

public class Test<TBase>
{
    private IList<Expression<Func<object, object>>> _expressions = new List<Expression<Func<object, object>>>();

    public void AddExpression<T>(Expression<Func<TBase, T>> e)
    {
        _expressions.Add(e);
    }

    public void AddExpression<T1, T2>(Expression<Func<T1, T2>> e)
    {
        _expressions.Add(e);
    }
}

我需要/想要保留一个表达式列表,其中Func内的类型可能会有所不同。我虽然上面的代码可行,但它没有。它失败了:

  

Cannot convert from 'Expression<Func<TBase, T>>' to 'Expression<Func<object, object>>'

     

Cannot convert from 'Expression<Func<T1, T2>>' to 'Expression<Func<object, object>>'

Resharper说:

  

Argument type 'Expression<Func<TBase, T>>' is not assignable to parameter type 'Expression<Func<object, object>>'

     

Argument type 'Expression<Func<T1, T2>>' is not assignable to parameter type 'Expression<Func<object, object>>'

代码是否可以仅通过更改方法体而不是方法签名来工作?

1 个答案:

答案 0 :(得分:2)

  

更新:此问题被标记为重复,但虽然我理解代码的问题,但我没有解决方案。是否可以通过仅更改方法体而不是方法签名来使代码工作?

是的,您可以保留方法签名,但是您必须重写表达式...

像这样:

public void AddExpression<T1, T2>(Expression<Func<T1, T2>> e)
{
    var originalParameter = e.Parameters[0];

    // object par1
    var parameter = Expression.Parameter(typeof(object), originalParameter.Name);

    // T1 var1
    var variable = Expression.Variable(typeof(T1), "var1");

    // (T1)par1
    var cast1 = Expression.Convert(parameter, typeof(T1));

    // var1 = (T1)par1;
    var assign1 = Expression.Assign(variable, cast1);

    // The original body of the expression, with originalParameter replaced with var1
    var body = new SimpleParameterReplacer(originalParameter, variable).Visit(e.Body);

    // (object)body (a cast to object, necessary in the case T2 is a value type. If it is a reference type it isn't necessary)
    var cast2 = Expression.Convert(body, typeof(object));

    // T1 var2; var1 = (T1)par1; (object)body;
    // (the return statement is implicit)
    var block = Expression.Block(new[] { variable }, assign1, cast2);
    var e2 = Expression.Lambda<Func<object, object>>(block, parameter);

    _expressions.Add(e2);
}

我使用了我之前给出的另一个回复中的SimpleParameterReplacer

最后,(T1 x) => x.Somethingx.SomethingT2)转换为:

(object x) => 
{
    var var1 = (T1)x;
    return (object)var1.Something;
}