您的位置:首页 > 编程语言 > C#

.net(C#) 获取当前命名空间,类名,方法名的方法

2016-11-12 11:53 555 查看

.net(C#) 获取当前命名空间,类名,方法名的方法

/// <summary>
/// 获取当前命名空间,类名,方法名的方法
/// </summary>
/// <returns></returns>
public static 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 += "\n";

System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(true);
System.Reflection.MethodBase mb = st.GetFrame(1).GetMethod();
//取得父方法命名空间
str += mb.DeclaringType.Namespace ;
//取得父方法类名
str += mb.DeclaringType.Name ;
//取得父方法类全名
str += mb.DeclaringType.FullName ;
//取得父方法名
str += mb.Name ;
return str;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  .net c# 方法名