using System.Reflection使用MethodInfo類別或是MethodBase類別中的GetCurrentMethod方法來取得Method名稱
//取得命名空間名稱 string sDllName = MethodInfo.GetCurrentMethod().Module.Name; //取得類別名稱 string sClassName = MethodInfo.GetCurrentMethod().ReflectedType.Name; //取得方法名稱 string sMethodName = MethodInfo.GetCurrentMethod().Name;
一般較常會使用到的地方:
1. 系統發生Exception地方,需記錄知道哪一個Method發生問題
2. 系統Kernel的地方,以方便了解資料進出是發生在哪一個Method
Example
try { // do something } catch (System.Exception ex) { MessageBox.Show(ex.ToString(), string.Format("[ {0} ] {1}::Exception", MethodInfo.GetCurrentMethod().ReflectedType.Name, MethodInfo.GetCurrentMethod().Name)); }

沒有留言:
張貼留言