How do I get the executing object for a stackframe?

后端 未结 3 1223
轻奢々
轻奢々 2021-01-01 22:55

When using reflection it is possible to obtain the call stack (apart from that it can be a crude approximation due to JIT optimizations) using System.Diagnostics.StackTrace

3条回答
  •  余生分开走
    2021-01-01 23:52

    I am not sure that I fully understand what you want, but if you want to know the type in which the method for a certain stack frame is declared, I think this code returns that:

    StackTrace trace = new StackTrace();    
    Type methodOwner = trace.GetFrame(0).GetMethod().DeclaringType;
    

    You will of course need to pass the index for the frame that you are interested in (I use 0 as example).

提交回复
热议问题