'MyClass' 类型的 ParameterExpression 不能用于 'System.Object' 类型的委托参数

ParameterExpression of type 'MyClass' cannot be used for delegate parameter of type 'System.Object'

我构建了一个 Func<object,bool> 类型的表达式树。但是当我尝试编译它时出现异常:

ParameterExpression of type 'MyClass' cannot be used for delegate parameter of type 'System.Object'

调用对象的类型在 运行 之前是未知的。

Expression leftFunc, rightFunc;
var x = Expression.Parameter(t);
if (left_element.All(char.IsDigit)) 
    leftFunc = Expression.Constant(int.Parse(left_element));
else 
    leftFunc = Expression.PropertyOrField(x, left_element);
if (right_element.All(char.IsDigit)) 
    rightFunc = Expression.Constant(int.Parse(right_element));
else 
    rightFunc = Expression.PropertyOrField(x, right_element);
var result = Expression.Lambda<Func<object, bool>>(
        Expression.GreaterThan(leftFunc, rightFunc), x); //exception thrown on this line
return result;

xMyClass 类型,我能以某种方式使用转换器吗?

看完评论更新。

完整代码,有效:

void Main()
{
    var method = something(typeof(MyClass), "propA", "propB");
    var classes = new List<MyClass>();
    classes.Add(new MyClass { propA = 1, propB = 2 }); // Should return false
    classes.Add(new MyClass { propA = 3, propB = 2 }); // Should return true
    classes.Add(new MyClass { propA = 2, propB = 2 }); // Should return false
    var res = classes.Where(method);
    res.Dump(); //Only class with propA = 3 && propB == 2 is returned
}

private Func<object, bool> something(Type t, string left_element, string right_element)
{
    var props = t.GetProperties();
    return (onObject) => {
        int left_int;
        object leftSide;
        if (!int.TryParse(left_element, out left_int))
        {
            leftSide = props.FirstOrDefault (p => p.Name == left_element).GetValue(onObject);
        } else {
            leftSide = left_int;
        }

        int right_int;
        object rightSide;
        if (!int.TryParse(right_element, out right_int))
        {
            rightSide = props.FirstOrDefault (p => p.Name == right_element).GetValue(onObject);
        } else {
            rightSide = left_int;
        }

        return Comparer.Default.Compare(leftSide, rightSide) > 0;       
    };
}

private class MyClass {
    public int propA {get;set;}
    public int propB {get;set;}
}

我以为你期待这样的事情。我使用泛型来避免你的参数异常

Expression Example<T>(T customeType)
{
    Expression leftFunc, rightFunc;
    var x = Expression.Parameter(typeof(T), "x");

    string left_element = "Length";
    string right_element = "Length";

    if (left_element.All(char.IsDigit))
        leftFunc = Expression.Constant(int.Parse(left_element));
    else
        leftFunc = Expression.PropertyOrField(x, left_element);
    if (right_element.All(char.IsDigit))
        rightFunc = Expression.Constant(int.Parse(right_element));
    else
        rightFunc = Expression.PropertyOrField(x, right_element);
    var result = Expression.Lambda<Func<T, bool>>(
            Expression.GreaterThan(leftFunc, rightFunc), x); 
    return result;
}