Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

reflection

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
like image 239
DaveDev Avatar asked Nov 09 '10 10:11

DaveDev


2 Answers

Try

var methodInfo = System.Reflection.MethodBase.GetCurrentMethod();
var fullName = methodInfo.DeclaringType.FullName + "." + methodInfo.Name;
like image 134
Samuel Jack Avatar answered Nov 16 '22 18:11

Samuel Jack


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;
like image 26
Patko Avatar answered Nov 16 '22 17:11

Patko