I’m using jqGrid to display some data to users. jqGrid has search functionality that does string compares like Equals, NotEquals, Contains, StartsWith, NotStartsWith, etc.
When I use StartsWith I get valid results (looks like this):
Expression condition = Expression.Call(memberAccess,
typeof(string).GetMethod("StartsWith"),
Expression.Constant(value));
Since DoesNotStartWith doesn’t exist I created it:
public static bool NotStartsWith(this string s, string value)
{
return !s.StartsWith(value);
}
This works, and I can create a string and call this method like so:
string myStr = "Hello World";
bool startsWith = myStr.NotStartsWith("Hello"); // false
So now I can create/call the expression like so:
Expression condition = Expression.Call(memberAccess,
typeof(string).GetMethod("NotStartsWith"),
Expression.Constant(value));
But I get a ArgumentNullException was unhandled by user code: Value cannot be null. error.
Parameter name: method
Does anyone know why this doesn’t work or a better way to approach this?
You’re checking for method
NotStartsWithon type string, which doesn’t exist. Instead oftypeof(string), trytypeof(ExtensionMethodClass), using the class where you put yourNotStartsWithextension method. Extension methods don’t actual exist on the type itself, they just act like they do.Edit: Also rearrange your
Expression.Callcall like this,The overload you are using expects an instance method, this overload expects a static method, based on the SO post you referred to. See here, http://msdn.microsoft.com/en-us/library/dd324092.aspx