1

jqGrid用来向用户显示一些数据。jqGrid 具有搜索功能,可以进行字符串比较,如 Equals、NotEquals、Contains、StartsWith、NotStartsWith 等。

当我使用时,StartsWith我得到了有效的结果(看起来像这样):

Expression condition = Expression.Call(memberAccess,
                typeof(string).GetMethod("StartsWith"),
                Expression.Constant(value));

由于 DoesNotStartWith 不存在,我创建了它:

public static bool NotStartsWith(this string s, string value)
{
    return !s.StartsWith(value);
}

这行得通,我可以创建一个字符串并像这样调用这个方法:

string myStr = "Hello World";
bool startsWith = myStr.NotStartsWith("Hello"); // false

所以现在我可以像这样创建/调用表达式:

Expression condition = Expression.Call(memberAccess,
                typeof(string).GetMethod("NotStartsWith"),
                Expression.Constant(value));

但我得到一个ArgumentNullException was unhandled by user code: Value cannot be null. Parameter name: method错误。

有谁知道为什么这不起作用或更好的方法来解决这个问题?

4

2 回答 2

5

您正在检查NotStartsWith类型字符串的方法,该方法不存在。而不是typeof(string), try typeof(ExtensionMethodClass),使用您放置NotStartsWith扩展方法的类。扩展方法实际上并不存在于类型本身上,它们只是像它们一样工作。

编辑:也Expression.Call像这样重新安排你的电话,

Expression condition = Expression.Call(
            typeof(string).GetMethod("NotStartsWith"),
            memberAccess,
            Expression.Constant(value));

您正在使用的重载需要一个实例方法,这个重载需要一个静态方法,基于您提到的 SO 帖子。见这里, http: //msdn.microsoft.com/en-us/library/dd324092.aspx

于 2011-08-15T19:04:46.097 回答
0

我知道这个问题得到了回答,但另一种方法可用且简单:

Expression condition = Expression.Call(memberAccess,
                                       typeof(string).GetMethod("StartsWith"),
                                       Expression.Constant(value));

condition = Expression.Not(condition);

并做了!只需要否定表达式。

于 2012-08-19T02:51:25.873 回答