Getting the instance that called the method in C#

后端 未结 5 1415
有刺的猬
有刺的猬 2021-02-18 16:05

I am looking for an algorithm that can get the object that called the method, within that method.

For instance:

public class Class1 {

    public void Me         


        
5条回答
  •  清歌不尽
    2021-02-18 16:32

    You could get to the current stack trace in code and walk up one step. http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx

    But as was commented below, this will get you the method and class calling you, but not the instance (if there is one, could be a static of course).

提交回复
热议问题