This is Expression<Func<string>> , so you can:
void Foo(Expression<Func<string>> selector) {...}
or
void Foo<T>(Expression<Func<T>> selector) {...}
however, note that the syntax of MyClass.Name refers to a static property; if you want an instance property, you might need something like Expression<Func<MyClass,string>> - for example:
static void Foo<TSource, TValue>( Expression<Func<TSource, TValue>> selector) { } static void Main() { Foo((MyClass obj) => obj.Name); }
As for the implementation; in this simple case , we can expect that Body will be MemberExpression , therefore:
static void Foo<TSource, TValue>( Expression<Func<TSource, TValue>> selector) { var member = ((MemberExpression)selector.Body).Member; Console.WriteLine(member.ReflectedType.Name + "." + member.Name); }
However, in the general case, it is more complicated. This will also work if we use a static member:
static void Foo<TValue>( Expression<Func<TValue>> selector) { var member = ((MemberExpression)selector.Body).Member; Console.WriteLine(member.ReflectedType.Name + "." + member.Name); }
source share