开发者

C# Reflection: Get calling class' Type [duplicate]

This question already has answers her开发者_JAVA百科e: Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜