获取在C#中调用该方法的实例 [英] Getting the instance that called the method in C#

查看:236
本文介绍了获取在C#中调用该方法的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种可以在该方法内获取调用该方法的对象的算法.

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

例如:

public class Class1 {

    public void Method () {
        //the question
        object a = ...;//the object that called the method (in this case object1)
        //other instructions
    }

}

public class Class2 {

    public Class2 () {
        Class1 myClass1 = new Class1();
        myClass1.Method();
    }

    public static void Main () {
        Class2 object1 = new Class2();
        //...
    }

}

有什么办法吗?

推荐答案

很显然,我不知道您所处情况的确切细节,但这似乎确实需要您重新考虑一下结构.

Obviously i don't know the exact details of your situation but this really seems like you need to rethink your structure a bit.

如果结构正确,继承就很容易做到.

This could easily be done if proper inheritance is structured.

考虑研究一个抽象类和从该抽象类继承的类.您甚至可以通过界面来完成相同的事情.

Consider looking into an abstract class and classes that inherit from said abstract class. You might even be able to accomplish the same thing with interfaces.

这篇关于获取在C#中调用该方法的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆