可能重复:
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()
内,我如何获得调用Type
的班级Foo
?
答案 0 :(得分:6)
你没有高效率。作为一个设计点,我认为最好将调用类类型传递给Pirate方法。
如果你不能将类类型作为参数传递,那么你可以从System.Diagnostics命名空间中的StackTrace类获取调用类型,但是如果我的内存是正确的,那么它是一个相当昂贵的类使用