获取调用C#方法的实例

发布于 2024-08-21 03:29:20 字数 506 浏览 2 评论 0原文

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

例如:

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();
        //...
    }

}

有什么办法可以做到这一点吗?

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 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();
        //...
    }

}

Is there any way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩物 2024-08-28 03:29:20

这是如何执行此操作的示例...

...
using System.Diagnostics;
...

public class MyClass
{
/*...*/
    //default level of two, will be 2 levels up from the GetCaller function.
    private static string GetCaller(int level = 2)
    {
        var m = new StackTrace().GetFrame(level).GetMethod();

        // .Name is the name only, .FullName includes the namespace
        var className = m.DeclaringType.FullName;

        //the method/function name you are looking for.
        var methodName = m.Name;

        //returns a composite of the namespace, class and method name.
        return className + "->" + methodName;
    }

    public void DoSomething() {
        //get the name of the class/method that called me.
        var whoCalledMe = GetCaller();
        //...
    }
/*...*/
}

发布此内容是因为我花了一段时间才找到我自己正在寻找的内容。我在一些静态记录器方法中使用它......

Here's an example of how to do this...

...
using System.Diagnostics;
...

public class MyClass
{
/*...*/
    //default level of two, will be 2 levels up from the GetCaller function.
    private static string GetCaller(int level = 2)
    {
        var m = new StackTrace().GetFrame(level).GetMethod();

        // .Name is the name only, .FullName includes the namespace
        var className = m.DeclaringType.FullName;

        //the method/function name you are looking for.
        var methodName = m.Name;

        //returns a composite of the namespace, class and method name.
        return className + "->" + methodName;
    }

    public void DoSomething() {
        //get the name of the class/method that called me.
        var whoCalledMe = GetCaller();
        //...
    }
/*...*/
}

Posting this, because it took me a while to find what I was looking for myself. I'm using it in some static logger methods...

纵山崖 2024-08-28 03:29:20

您可以在代码中找到当前的堆栈跟踪并向上移动一步。
http://msdn.microsoft.com/en-us/ library/system.diagnostics.stacktrace.aspx

但正如下面评论的那样,这将为您提供调用您的方法和类,但不是实例(如果有的话,当然可能是静态的)。

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).

时光沙漏 2024-08-28 03:29:20

或者只是将对象作为方法参数传递。

public void Method(object callerObject)
{
..
}

并调用方法:

myClass.Method(this);

问候,弗洛里安

or just pass the object as method parameter.

public void Method(object callerObject)
{
..
}

and call the Method:

myClass.Method(this);

regards, Florian

醉态萌生 2024-08-28 03:29:20

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

如果构建适当的继承,这可以很容易地完成。

考虑研究一个抽象类和从该抽象类继承的类。您甚至可以使用接口来完成同样的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文