3

Imagine I have a static class and a static method inside that. And it has to be accessed by 10 different classes. But how the static class will know who has called it :(

It was an interview question....please rephrase it properly and answer me, I am new :(

Alex
  • 34,899
  • 5
  • 77
  • 90
Jasmine
  • 5,186
  • 16
  • 62
  • 114

5 Answers5

2

As C# does not have a proper metaobject system, the only way I know of is via reflection. The following idea should give the idea:

public static string GetCaller()
{
    var trace = new StackTrace(2);
    var frame = trace.GetFrame(0);
    var caller = frame.GetMethod();
    var callingClass = caller.DeclaringType.Name;
    var callingMethod = caller.Name;
    return String.Format("Called by {0}.{1}", callingClass, callingMethod);
}
Stefan Nobis
  • 957
  • 4
  • 12
  • Thank you Stefan. But I got the result for your program as " Called by AppDomain.nExecuting Assembly" :( – Jasmine Jul 21 '12 at 18:13
  • The above code should show you the idea. Play with the parameters. The most important one is the parameter to the constructor of StackTrace. Try for example `var trace = new StackTrace(1)`. And have a look at the documentation for the constructor. – Stefan Nobis Jul 22 '12 at 15:42
  • For me, StackTrace stackTrace = new StackTrace(); var method = stackTrace.GetFrame(1).GetMethod(); was working. From the method (above "caller") you can use above example again. – Andreas Reiff May 31 '23 at 12:13
1

I would try the following:

public class ParentClass
{
}

public class ChildClass :  ParentClass
{
}

public static class StaticClass
{
    public static void SomeMethod(ParentClass d)
    {
        var t = d.GetType();
    }
}

public class StaticChildren
{
    public void Children()
    {
        var p = new ChildClass();

        StaticClass.SomeMethod(p);

    }
}

Just passing an instance is the simplest you can do here.

Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • I probably would use an object since this will be called from 10 different classes – Mark Hall Jul 21 '12 at 17:14
  • What the heck? Edited in the complete example, this works for all subclasses of course. – Mare Infinitus Jul 21 '12 at 17:17
  • @MareInfinitus: You rock ;) Cool thank you so much, but tell me, why it gives me the type as "test.Program+derived+ChildClass" ? I know test.program is my class. But I dont get what is derived and why + and all comes here :( Also I have a doubt, I made this staticchildren class as static, and its member "Children" also static and I saw no difference when I just declared the method "Children" as static...How it differs in performance ? – Jasmine Jul 21 '12 at 18:09
  • @MareInfinitus: Hey I observed, if the type is not of Parentclass, as obviously, if I have any other class, it doesnt works ....What do you suggest for an optimized solution in this case ? – Jasmine Jul 21 '12 at 18:44
  • @MareInfinitus: Absolutely right,I then used object it worked right perfectly. Thanks a lot :) Cheers – Jasmine Jul 21 '12 at 19:15
0

You could use the stracktace to find out who called the static method!

class Foo
{
  public void static staticMethod()
  {
    // here i want to know who called me!
    StackTrace st = new StackTrace();
    ...
  }
}

class Bar
{
  public void Bar()
  {
    Foo.staticMethod();
  }
}
0

In these cases you can use Reflection.

Find more about reflection under these links: http://www.csharp-examples.net/reflection-calling-method-name/

http://msdn.microsoft.com/en-us/library/ms173183(v=vs.80).aspx

eridanix
  • 818
  • 1
  • 8
  • 27
0

If the functionality of a method is dependent on who called it, then the design is probably not very good. I'd introduce new parameters instead.

for debugging purposes, a stack trace?

Erix
  • 7,059
  • 2
  • 35
  • 61