Determine Calling Object Type in C#

前端 未结 11 845
灰色年华
灰色年华 2021-02-05 14:37

Regardless of whether or not this is a good idea, is it possible to implement an interface where the executing function is aware of the calling object\'s type?

c         


        
11条回答
  •  隐瞒了意图╮
    2021-02-05 14:40

    You could use the System.Diagnostics.StackTrace class to create a stack trace. Then you could look for the StackFrame that is associated with the caller. The StackFrame has a Method property that you can use to get to the type of the caller.

    However, the above method is nothing that should be used in performance critical code.

提交回复
热议问题