|
|
异常处理时获取方法名的方法(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;
}
|
|
|
|
|
|