Using System.Reflection to Get a Method's Full Name

I have a class that look like the following:

public class MyClass
{

...

    protected void MyMethod()
    {
    ...
    string myName = System.Reflection.MethodBase.GetCurrentMethod.Name;
    ...
    }

...

}

The value of myName is "MyMethod".

Is there a way that I can use Reflection to get a value of "MyClass.MyMethod" for myName instead?


You could look at the ReflectedType of the MethodBase you get from GetCurrentMethod, i.e.,

MethodBase method = System.Reflection.MethodBase.GetCurrentMethod();
string methodName = method.Name;
string className = method.ReflectedType.Name;

string fullMethodName = className + "." + methodName;

And to get the full method name with parameters:

var method = System.Reflection.MethodBase.GetCurrentMethod();
var fullName = string.Format("{0}.{1}({2})", method.ReflectedType.FullName, method.Name, string.Join(",", method.GetParameters().Select(o => string.Format("{0} {1}", o.ParameterType, o.Name)).ToArray()));

I think these days, it's best to do this:

string fullMethodName = $"{typeof(MyClass).FullName}.{nameof(MyMethod)}";

Extending Ruben's, you can get the full name like this:

var info = System.Reflection.MethodBase.GetCurrentMethod();
var result = string.Format(
                 "{0}.{1}.{2}()",
                 info.ReflectedType.Namespace,
                 info.ReflectedType.Name,
                 info.Name);

You can add it to a static method that receives a MethodBase parameter and generates the string.