简体   繁体   English

如何获取具有命名空间和类名的方法名称?

[英]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. 您可能需要GetCurrentMethod()。DeclaringType ,它返回一个Type对象,该对象包含有关声明该方法的类的信息。 Then you can use FullName property to get the namespace. 然后,您可以使用FullName属性来获取命名空间。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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