zoukankan      html  css  js  c++  java
  • 获取到某一方法的调用者的类名、方法名、命名空间(转)

    1、返回当前方法所在的类名:

    using System.Reflection;
    string className = MethodBase.GetCurrentMethod().ReflectedType.Name;

    2、返回调用当前方法的方法名:

    using System.Diagnostics;
    using System.Reflection;
    
    StackTrace   trace   =   new   StackTrace();   
    MethodBase   methodName =  trace.GetFrame(1).GetMethod();
    3、例子
        在Program类Main方法中调用TestCodon.Test方法
        class Program
        {
            static void Main(string[] args)
            {
    
                TestCodon _tt = new TestCodon();
                _tt.Test();
     
                Console.ReadKey();
            }
        }
        public class TestCodon : AbstractCodon
        {
            public void Test()
            {
                StackTrace trace = new StackTrace();
                MethodBase methodName = trace.GetFrame(1).GetMethod();
                
                Console.WriteLine(methodName.DeclaringType.FullName+"."+methodName.Name);
            }
         }

    转自 Kevin 的博客

  • 相关阅读:
    lua 与 c 的相互调用
    平台认证 & HTTP 302 重定向
    1. 个人经验总结
    Java反编译
    1. 个人经验总结
    3. 技术专题
    Office
    工作机的目录组织
    Eclipse
    Eclipse中的Gradle集成
  • 原文地址:https://www.cnblogs.com/mrhgw/p/2053035.html
Copyright © 2011-2022 走看看