Alex Rivera | Logout

Lambda and Expression.Call for an extension method

Asked 2011-12-01T07:17:34.193
8

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?

Edit
Report

1 Answer

-1

If you want to get your extension method worked you must do like this:

string str = "some string";
str.Like("second string");
answered 2011-12-01T07:26:10.483

Your Answer