I need to implement an expression for a method like here:
var prop = Expression.Property(someItem, "Name");
var value = Expression.Constant(someConstant);
var contains = typeof(string).GetMethod("Contains", new[] {typeof(string)});
var expression = Expression.Call(prop, contains, value);
But for my extension method:
public static class StringEx
{
public static bool Like(this string a, string b)
{
return a.ToLower().Contains(b.ToLower());
}
}
Unfortunately, next code throws an ArgumentNullException for a parameter "method":
var like = typeof(string).GetMethod("Like", new[] {typeof(string)});
comparer = Expression.Call(prop, like, value);
What I'm doing wrong?
I am not sure, but you can only get an extension method from the static class using reflection. Extension methods are not truly added to the class, therefore can't be retrieved with
GetMethod
.Try this
Use
ie. retrieve it from the extending type, not from the extended type.
If you want to get your extension method worked you must do like this:
You can do like this:
You can pass
prop
as first parameter andvalue
as second parameter like above.Maybe you will need to get a complete query before apply an extension method.