c# 获取方法所在的命名空间 类名 方法名
生活随笔
收集整理的這篇文章主要介紹了
c# 获取方法所在的命名空间 类名 方法名
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
平時我們在記錄日志的時候難免會需要直接記錄當前方法的路徑,以便查找,但是每次都輸入方法名稱非常的繁瑣,同時如果修改了方法名稱也要去手動修改日志內容,真的是勞命傷財啊,所以有了如下方法則可解決我們的大難題啊,閑話少說,直接上代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Reflection;namespace GetMethodNameSpace {class Program{public static string GetMethodInfo(){string str = "";//取得當前方法命名空間str += "命名空間名:" + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "\n";//取得當前方法類全名str += "類名:" + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + "\n";//取得當前方法名str += "方法名:" + System.Reflection.MethodBase.GetCurrentMethod().Name + "\n";str += "\n";StackTrace ss = new StackTrace(true);MethodBase mb = ss.GetFrame(1).GetMethod();//取得父方法命名空間str += mb.DeclaringType.Namespace + "\n";//取得父方法類名str += mb.DeclaringType.Name + "\n";//取得父方法類全名str += mb.DeclaringType.FullName + "\n";//取得父方法名str += mb.Name + "\n";return str;}public static void Main(){Console.WriteLine(GetMethodInfo());Console.ReadKey();}} }?提取公共方法如下:
?
轉載于:https://www.cnblogs.com/duanjt/p/5462798.html
總結
以上是生活随笔為你收集整理的c# 获取方法所在的命名空间 类名 方法名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下mysql中文乱码
- 下一篇: c# char unsigned_dll