Absolutely - but you'll want a method signature like this:
public static string GetMethodName<T>(Expression<Action<T>> action)
(That means you'll need to specify the type argument when you call it, in order to use a lambda expression.)
Sample code:
using System;
using System.Linq.Expressions;
class Test
{
void Foo()
{
}
static void Main()
{
string method = GetMethodName<Test>(x => x.Foo());
Console.WriteLine(method);
}
static string GetMethodName<T>(Expression<Action<T>> action)
{
MethodCallExpression methodCall = action.Body as MethodCallExpression;
if (methodCall == null)
{
throw new ArgumentException("Only method calls are supported");
}
return methodCall.Method.Name;
}
}