当前位置:  -> 首页 -> 异常处理时获取方法名的方法(C#)

上一篇 | 下一篇
异常处理时获取方法名的方法(C#)
作者:洛羽叶  点击率:2018  发布时间:2012-03-21
依靠Exception自身的属性获取方法名在跨域或者跨工程时容易造成调用链信息丢失,为了保证调用链的完整,方便进行代码级别的排错,需要自身构建调用链属性。
以下是两种获取方法名的方法,通过环境对象的调用链获取:
 
Environment.StackTrace;
public static string GetCurrentMethodName(string strCallStack)
{

string strTemp;
        
trTemp = strCallStack.Substring(strCallStack.IndexOf(
"get_StackTrace"0) + 23);
strTemp=strTemp.Substring(
0,strTemp.IndexOf("("));           
return strTemp;  

}
 
通过反射的类方法属性来获取:
 
System.Reflection.MethodBase.GetCurrentMethod();

public static string GetCurrentMethodName(MethodBase method)
{

string strTemp;
 
strTemp = method.DeclaringType.FullName + 
"." + method.Name;
return strTemp;

}                
标签: C# 异常 反射 .NET
引用地址:本站原创
   站点首页      技术人生      旅途足迹      我要留言      友情链接      关于站长   
[本站统计]
在线人数:23
今日访问:536
总访问量:1936367
Copyright 2006-2022 EasyWeb 1.6 订阅 All Rights Reserved
粤ICP备08028977号-1
www.luoriver.com
Created by WWH in 2006