Search code examples
c#reflectiondelegatesstatic-methods

Get non-static method name inside static method


I am trying to do something rather simple but not sure if there's any way around simply creating a dummy instance of my class.

I am trying to simply get the method name of a non-static method using some simple code:

static string GetMethodName(Func<string, int> function  )
        {
            return function.Method.Name;
        }

however I am trying to call this from MyStaticMethod like so and of course it is complaining:

private static void MyStaticMethod()
{
   var a = GetMethodName(MyNonStaticMethod);
}


private int MyNonStaticMethod(string param1)
{
  return 0;
}

Is there any way to accomplish this without creating a dummy instance of the containing class? obviously my case is more complex and I cannot simply make my non static method static (it requires an instance and has dependency bindings). Just wondering if this is possible as all I need is the name (so really don't need an instance). I am trying to get away from magic strings and want some compile time errors when things change.

edit: I've created a static helper class

i have a generic method:

    public static string GetMemberName<T>(
                Expression<Func<T, object>> expression)
            {
                if (expression == null)
                {
                    throw new ArgumentNullException("expression");
                }

                return _GetMemberName(expression.Body);
            }

private static string _GetMemberName(
            Expression expression)
        {   
            if (expression is MemberExpression)
            {
                var memberExpression =
                    (MemberExpression)expression;
                return memberExpression.Member.Name;
            }

            if (expression is MethodCallExpression)
            {
                var methodCallExpression = (MethodCallExpression)expression;
                return methodCallExpression.Method.Name;
            }

            if (expression is UnaryExpression)
            {
                var unaryExpression = (UnaryExpression)expression;
                return GetMemberName(unaryExpression);
            }

            throw new ArgumentException("Unrecognized expression");
        }

Solution

  • Of course you can do this. Use Expression<Func<YourInstanceClass, TReturn>> like this:

    static string GetMethodName<TReturn>(Expression<Func<YourInstanceClass, TReturn>> function)
    {
        var call = function.Body as MethodCallExpression;
        return call != null ? call.Method.Name : "not a single call expression";
    }
    

    now you can

    var name = GetMethodName(a => a.MyNonStaticMethod("1"));
    
    Console.WriteLine (name); //prints MyNonStaticMethod
    

    where

    public class YourInstanceClass
    {
        public int MyNonStaticMethod(string param1)
        {
            return 0;
        }
    }
    

    I've made MyNonStaticMethod public, so that I can call it outside, but you can left it private and call it in static method inside a class