Tôi chỉ mới bắt đầu với các cây biểu thức vì vậy tôi hy vọng điều này có ý nghĩa. Tôi cố gắng để tạo ra một cây biểu thức đại diện:Làm cách nào để tạo cây biểu thức đại diện cho 'String.Contains ("term")' trong C#?
t => t.SomeProperty.Contains("stringValue");
Cho đến nay tôi đã có:
private static Expression.Lambda<Func<string, bool>> GetContainsExpression<T>(string propertyName, string propertyValue)
{
var parameterExp = Expression.Parameter(typeof(T), "type");
var propertyExp = Expression.Property(parameter, propertyName);
var containsMethodExp = Expression.*SomeMemberReferenceFunction*("Contains", propertyExp) //this is where I got lost, obviously :)
...
return Expression.Lambda<Func<string, bool>>(containsMethodExp, parameterExp); //then something like this
}
Tôi chỉ không biết làm thế nào để tham khảo các phương pháp String.Contains().
Giúp đánh giá cao.
gì nếu tôi muốn gọi không startWith "chuỗi con"? – amd
@AhmD Expression.Not xung quanh Expression.Call, và thay đổi GetMethod thành StartsWith? –
cảm ơn điều này chính xác những gì cần thiết! – amd