DaveDev
DaveDev

Reputation: 42175

How can I get a method name with the namespace & class name?

I'd like to get the fully qualified method name. I can see how to get the method name on its own with the following:

System.Reflection.MethodBase.GetCurrentMethod().Name

but this only returns the actual name. I need everything, like:

My.Assembly.ClassName.MethodName

Upvotes: 43

Views: 21175

Answers (2)

Patko
Patko

Reputation: 4423

You probably want the GetCurrentMethod().DeclaringType, which returns a Type object that holds information about the class which declares the method. Then you can use FullName property to get the namespace.

var currentMethod = System.Reflection.MethodBase.GetCurrentMethod();
var fullMethodName = currentMethod.DeclaringType.FullName + "." + currentMethod.Name;

Upvotes: 23

Samuel Jack
Samuel Jack

Reputation: 33260

Try

var methodInfo = System.Reflection.MethodBase.GetCurrentMethod();
var fullName = methodInfo.DeclaringType.FullName + "." + methodInfo.Name;

Upvotes: 60

Related Questions