-1

Possible Duplicate:
Reflection in C#: How do I get the calling method name and type?

Let's say I have two classes:

public class Foo {
    public Foo() {
        Bar.Pirate();
    }
}

public static class Bar {
    public static void Pirate() {
        Type callingClassType = ...
    }
}

Within Pirate(), how do I get the Type of the class (Foo) that called Pirate()?

Community
  • 1
  • 1
FoobarisMaximus
  • 1,109
  • 3
  • 13
  • 18
  • 3
    http://stackoverflow.com/questions/3095696/reflection-in-c-how-do-i-get-the-calling-method-name-and-type Although, I think looking into the stack trace is a bad idea. – Marc Jun 15 '11 at 14:38
  • I'm curious as to why you want this functionality. – Coeffect Jun 15 '11 at 14:41

1 Answers1

5

You don't get it efficiently. As a point of design, I think it's better to pass the calling class type into the Pirate method.

If you can't pass the class type as a parameter then you can get the calling type from the StackTrace class in the System.Diagnostics namespace, but if my memory is correct it's a fairly expensive class to use.

John Bledsoe
  • 17,142
  • 5
  • 42
  • 59