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;
}
复制代码
posted @   MaxBruce  阅读(2021)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.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分库分表方案?
点击右上角即可分享
微信分享提示