Lambda und Ausdruck.Rufen Sie für eine Erweiterung Methode

Brauche ich zur Umsetzung ein Ausdruck für eine Methode wie hier:

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);

Aber für meine extension Methode:

public static class StringEx
{
    public static bool Like(this string a, string b)
    {
        return a.ToLower().Contains(b.ToLower());
    }
}

Leider, nächsten code löst eine ArgumentNullException-Ausnahme für einen parameter "Methode":

var like = typeof(string).GetMethod("Like", new[] {typeof(string)});
comparer = Expression.Call(prop, like, value);

Was ich falsch mache?

InformationsquelleAutor CodeAddicted | 2011-12-01
Schreibe einen Kommentar