score:1

Accepted answer

i don't understand what you are doing, i think you could probably completely avoid some of the code you show here.

i wrote this "getmembername" extension method, you probably can do something with this code:

public static string getmembername<t, tresult>(
    this t anyobject, 
    expression<func<t, tresult>> expression)
{
    return ((memberexpression)expression.body).member.name;
}

// call as extension method, if you have a instance
string lengthpropertyname = "abc".getmembername(x => x.length);

// or call as a static method, by providing the type in the argument
string lengthpropertyname = reflectionutility.getmembername(
    (string x) => x.length);

edit:

just to sketch up a solution:

public static bool ismethod<tresult>(
  methodinfo method, 
  expression<func<tresult>> expression)
{
  // i think this doesn't work like this, evaluate static method call
  return method == ((memberexpression)expression.body).member;
}

if (ismethod(expr.method, () => sqlfilterextensions.like))
{
  // generate sql
}

score:0

if you have control over the like method, maybe you could work from there directly instead of inspecting the expressions later.

if you don't have control over the method there is no other way than to do it by comparing the name

score:1

  1. it wouldn't fail silently if you had unit tests to test it.
  2. if you used resharper, it would offer to change the text in the string literal at the same time as renaming the method.

Related Query

More Query from same tag