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()
?
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.
精彩评论