我遇到了一些挑战,我必须创建一个表达式树来表示用户的查询输入。由于我没有时间创建所有可能的用户输入案例,我认为表达式树可以帮助我解决这个问题。
在大多数情况下,它有。然而,我有点难过。我在下面的代码中尝试使用动态创建的表达式执行 List.Find。简而言之,表达式是这样的:
list.Find(m => m.ListOfStrings.Exists(s => s == "cookie"));
其中 m 是
class MyClass
{
public List<string> ListOfStrings { get; set; }
}
我已经做到了创造
s => s == "cookie"
有表情,没问题。我还为 Exists 声明了一个方法信息
var existsMethod = typeof(MyClass)
.GetProperty("ListOfStrings")
.PropertyType
.GetMethod("Exists");
我唯一的问题是创建一个表达式来调用所述方法,将 lambda 作为参数,如下所示
var findLambda = Expression.Lambda(
Expression.Call(
Expression.Property(
Expression.Parameter(typeof(MyClass), "m"),
typeof(MyClass).GetProperty("ListOfStrings")),
existsMethod,
existsLambda),
Expression.Parameter(
typeof (MyClass),
"m"));
它给出了一个可以理解的例外
Expression of type 'System.Func`2[System.String,System.Boolean]' cannot be used for parameter of type 'System.Predicate`1[System.String]' of method 'Boolean Exists(System.Predicate`1[System.String])'
我怎么能克服这个?
完整代码:
private class MyClass
{
public List<string> ListOfStrings { get; set; }
}
public void SomeMethod()
{
var myObject = new MyClass();
myObject.ListOfStrings = new List<string>();
myObject.ListOfStrings.Add("cookie");
myObject.ListOfStrings.Add("biscuit");
List<MyClass> list = new List<MyClass>();
list.Add(myObject);
var existsLambda = Expression.Lambda(
Expression.Equal(
Expression.Parameter(typeof(string), "s"),
Expression.Constant("cookie")),
Expression.Parameter(typeof(string), "s"));
var existsMethod = typeof(MyClass).GetProperty("ListOfStrings").PropertyType.GetMethod("Exists");
var findLambda = Expression.Lambda(
Expression.Call(
Expression.Property(
Expression.Parameter(typeof(MyClass), "m"),
typeof(MyClass).GetProperty("ListOfStrings")),
existsMethod,
existsLambda),
Expression.Parameter(
typeof (MyClass),
"m"));
list.Find((Predicate<MyClass>)findLambda.Compile());
}