Getting the instance that called the method in C#

后端 未结 5 1572
有刺的猬
有刺的猬 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:37

    It would be very bad style since

    a) that would break encapsulation
    b) it's impossible to know the type of the calling object at compile-time so whatever you do with the object later, it will propably not work.
    c) it would be easier/better if you'd just pass the object to the constructor or the method, like:

    Class1 c1 = new Class1(object1);
    

提交回复
热议问题