MyClass'不能用于类型为'System.Object'的委托参数
本文关键字:参数 Object 类型 不能 用于 MyClass System | 更新日期: 2023-09-27 18:07:30
我已经构建了一个类型为Func<object,bool>
的表达式树。但是当我试图编译它时,我得到异常:
类型为"MyClass"的ParameterExpression不能用于类型为"System"的委托参数。对象'
调用对象的类型在运行时之前是未知的。
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;
x
是MyClass
类型,我可以用某种方式使用转换器吗?
阅读评论后更新
完整代码,工作:
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;
}