Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the instance that called the method in C#

Tags:

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?

like image 985
Willem Van Onsem Avatar asked Feb 10 '10 14:02

Willem Van Onsem


People also ask

How can I get the class name that calls my method?

Method Class | getName() Method in Java Method class is helpful to get the name of methods, as a String. To get name of all methods of a class, get all the methods of that class object. Then call getName() on those method objects. Return Value: It returns the name of the method, as String.

How do you access a method in C#?

Calling a method is like accessing a field. After the object name (if you're calling an instance method) or the type name (if you're calling a static method), add a period, the name of the method, and parentheses. Arguments are listed within the parentheses and are separated by commas.

What is instance method in C#?

An instance method is a method that belongs to instances of a class, not to the class itself. To define an instance method, just omit static from the method heading. Within the method definition, you refer to variables and methods in the class by their names, without a dot.


2 Answers

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

like image 92
Tracker1 Avatar answered Sep 30 '22 18:09

Tracker1


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

like image 20
Teun D Avatar answered Sep 30 '22 19:09

Teun D