简体   繁体   中英

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

Try

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM