【问题标题】:C# Reflection: Get calling class' Type [duplicate]C#反射:获取调用类的类型[重复]
【发布时间】:2011-09-15 14:40:59
【问题描述】:

可能重复:
Reflection in C#: How do I get the calling method name and type?

假设我有两个类:

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

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

Pirate() 中,我如何获得调用Pirate() 的类(Foo)的Type

【问题讨论】:

标签: c# reflection


【解决方案1】:

你没有有效地得到它。作为一个设计点,我认为最好将调用类类型传递给 Pirate 方法。

如果您不能将类类型作为参数传递,那么您可以从 System.Diagnostics 命名空间中的 StackTrace 类中获取调用类型,但如果我没记错的话,这是一个使用起来相当昂贵的类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-07
    • 2010-12-20
    • 1970-01-01
    • 2021-11-26
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多