开发者

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

开发者 https://www.devze.com 2023-03-13 16:22 出处:网络
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?
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

精彩评论

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