Public Shared Function getFilterStartsWith(Of T)(ByVal param As ParameterExpression, ByVal prop As MemberExpression, ByVal arg As Object) As Expression
Dim mi As MethodInfo = GetType(String).GetMethod("StartsWith", New Type() {GetType(String)}, Nothing)
Return Expression.Lambda(Of Func(Of T, Boolean))(LambdaExpression.[Call](prop, mi, Expression.Constant(arg, GetType(String))), param)
End Function
在 C# 中:
public static Expression getFilterStartsWith<T>(ParameterExpression param, MemberExpression prop, object arg) {
MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }, null);
return Expression.Lambda<Func<T, bool>>(LambdaExpression.Call(prop, mi, Expression.Constant(arg, typeof(string))), param);
}
这就是我在最近写的solution 中用于startsWith 的函数。事实证明这是一个巨大的痛苦,因为您不能将 Type 变量用作 ctype 中的参数或 vb 中的 DirectCast,并且您不能对同一类型的对象和可为空的对象进行 linq 比较。