C# 获取当前方法的名称空间、类名和方法名称
原文网址:http://t.zoukankan.com/cang12138-p-7714651.html
1.(new StackTrace()).GetFrame(1) // 0为本身的方法;1为调用方法
2.(new StackTrace()).GetFrame(1).GetMethod().Name; // 方法名
3.(new StackTrace()).GetFrame(1).GetMethod().ReflectedType.Name; // 类名
获取当前方法的名称空间、类名和方法名称
public string GetMethodInfo() { string str = ""; //取得当前方法命名空间 str += "命名空间名:" + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + " "; //取得当前方法类全名 包括命名空间 str += "类名:" + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + " "; //取得当前方法名 str += "方法名:" + System.Reflection.MethodBase.GetCurrentMethod().Name + " "; str += " "; //父方法 System.Diagnostics.StackTrace ss = new System.Diagnostics.StackTrace(true); System.Reflection.MethodBase mb = ss.GetFrame(1).GetMethod(); //取得父方法命名空间 str += mb.DeclaringType.Namespace + " "; //取得父方法类名 str += mb.DeclaringType.Name + " "; //取得父方法类全名 str += mb.DeclaringType.FullName + " "; //取得父方法名 str += mb.Name + " "; return str; }
分类:
C# 知识
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2021-11-16 详解MongoDB中的多表关联查询($lookup)
2021-11-16 数据库设计三大范式
2021-11-16 一、表的设计步骤
2021-11-16 C#简单操作MongoDB
2020-11-16 什么是TCP,什么是UDP,它们两者的区别? 三次握手
2020-11-16 Vue面试中,经常会被问到的面试题/Vue知识点整理
2020-11-16 mysql 数据库 分表后 怎么进行分页查询?Mysql分库分表方案?