Nic*_*len 356 c# introspection
我知道你能做到
this.GetType().FullName
Run Code Online (Sandbox Code Playgroud)
要得到
My.Current.Class
Run Code Online (Sandbox Code Playgroud)
但是我能得到什么呢
My.Current.Class.CurrentMethod
Run Code Online (Sandbox Code Playgroud)
Jam*_*Ide 499
System.Reflection.MethodBase.GetCurrentMethod().Name从方法中调用.
小智 347
using System.Diagnostics;
...
var st = new StackTrace();
var sf = st.GetFrame(0);
var currentMethodName = sf.GetMethod();
Run Code Online (Sandbox Code Playgroud)
或者,如果您想要一个帮助方法:
[MethodImpl(MethodImplOptions.NoInlining)]
public string GetCurrentMethod()
{
var st = new StackTrace();
var sf = st.GetFrame(1);
return sf.GetMethod().Name;
}
Run Code Online (Sandbox Code Playgroud)
更新了@stusmith的学分.
Han*_*ant 96
反射有躲避森林为树木的诀窍.准确快速地获取当前方法名称永远不会有问题:
void MyMethod() {
string currentMethodName = "MyMethod";
//etc...
}
Run Code Online (Sandbox Code Playgroud)
虽然重构工具可能无法自动修复它.
如果你完全不关心使用Reflection的(相当大的)成本,那么这个辅助方法应该是有用的:
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Reflection;
//...
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetMyMethodName() {
var st = new StackTrace(new StackFrame(1));
return st.GetFrame(0).GetMethod().Name;
}
Run Code Online (Sandbox Code Playgroud)
更新:C#版本5和.NET 4.5具有满足此常见需求的黄金解决方案,您可以使用[CallerMemberName]属性让编译器在字符串参数中自动生成调用方法的名称.其他有用的属性是[CallerFilePath]让编译器生成源代码文件路径,[CallerLineNumber]用于获取进行调用的语句的源代码文件中的行号.
Update2:我现在可以在答案的顶部提出的语法在C#版本6中工作,而不需要花哨的重构工具:
string currentMethodName = nameof(MyMethod);
Run Code Online (Sandbox Code Playgroud)
Zak*_*ria 34
我认为获得全名的最佳方式是:
this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name;
Run Code Online (Sandbox Code Playgroud)
或试试这个
string method = string.Format("{0}.{1}", MethodBase.GetCurrentMethod().DeclaringType.FullName, MethodBase.GetCurrentMethod().Name);
Run Code Online (Sandbox Code Playgroud)
Orw*_*ile 11
这不起作用吗?
System.Reflection.MethodBase.GetCurrentMethod()
Run Code Online (Sandbox Code Playgroud)
返回表示当前正在执行的方法的MethodBase对象.
命名空间:System.Reflection
汇编:mscorlib(在mscorlib.dll中)
http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod.aspx
您也可以使用MethodBase.GetCurrentMethod()它来阻止JIT编译器内联它所使用的方法.
更新:
这个方法包含一个特殊的枚举StackCrawlMark,根据我的理解,它将指定JIT编译器不应该内联当前方法.
这是我对与SSCLI中存在的枚举相关的评论的解释.评论如下:
// declaring a local var of this enum type and passing it by ref into a function
// that needs to do a stack crawl will both prevent inlining of the calle and
// pass an ESP point to stack crawl to
//
// Declaring these in EH clauses is illegal;
// they must declared in the main method body
Run Code Online (Sandbox Code Playgroud)
小智 5
好吧System.Reflection.MethodBase.GetCurrentMethod().Name不是一个很好的选择'因为它只会显示方法名称而无需其他信息.
就像string MyMethod(string str)上面的属性将返回只是MyMethod几乎不够.
最好使用System.Reflection.MethodBase.GetCurrentMethod().ToString()哪个将返回整个方法签名...
| 归档时间: |
|
| 查看次数: |
309160 次 |
| 最近记录: |