Look if a method is called inside a method using reflection

后端 未结 3 1024
情话喂你
情话喂你 2020-11-30 08:26

I\'m working with reflection and currently have a MethodBody. How do I check if a specific method is called inside the MethodBody?

Assembly assembly = Assemb         


        
3条回答
  •  情深已故
    2020-11-30 08:33

    One can use the StackTrace class:

    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
    System.Diagnostics.StackFrame sf = st.GetFrame(1); 
    Console.Out.Write(sf.GetMethod().ReflectedType.Name + "." + sf.GetMethod().Name); 
    

    The 1 can be adjusted and determines the number of frame you are interested in.

提交回复
热议问题