简体   繁体   English

.NET反射-查找定义静态成员的类型

[英].NET Reflection - Finding the type that defines a static member

I have a problem with reflection. 我对反射有疑问。 I need to find the type that instantiates a static member. 我需要找到实例化静态成员的类型。 My code looks like this: 我的代码如下所示:

    private class SimpleTemplate : PageTemplate
    {
        internal static readonly IPageProperty NameProperty =
            PropertyRepository.Register("Name");
    }

The PropertyRepository is a repository of properties (obviously). PropertyRepository是属性的存储库(显然)。 It keeps track of all the properties that have been registered using the type system that I'm building. 它跟踪使用我正在构建的类型系统注册的所有属性。

In order to do that successfully, I need to keep track of all the properties but also the type on which they are defined. 为了成功做到这一点,我需要跟踪所有属性以及定义它们的类型。 Otherwise, if two properties with the same name are defined, the property repository won't be able to tell them apart. 否则,如果定义了两个具有相同名称的属性,则属性存储库将无法区分它们。

So, what I want to do is to find out the type that defines the NameProperty and store the type as well as the name. 因此,我要做的是找出定义NameProperty的类型,并存储类型和名称。 How can I do that? 我怎样才能做到这一点?

I want to use strong typing, ie I do not want to send the type as an argument to PropertyRepository.Register. 我想使用强类型,即我不想将类型作为参数发送给PropertyRepository.Register。 That would be error-prone since I can't validate that the type argument is correct. 由于我无法验证type参数是否正确,因此容易出错。

The solution, I imagine, would involve reflection. 我认为解决方案将涉及反思。 Is there any way to use reflection to determine which type calls a static method? 有什么方法可以使用反射来确定哪种类型调用静态方法? The static properties are implicitly instantiated using a static constructor (that the compiler generates). 静态属性使用静态构造函数(由编译器生成)隐式实例化。 Is there a way for me to get a handle to that constructor? 我有办法获取该构造函数的句柄吗? That seems feasible, I just cannot figure out how to do that. 这似乎可行,我只是不知道该怎么做。

In other words: If method A calls method B, is there any way B can tell that it was called from A using reflection? 换句话说:如果方法A调用方法B,则B是否有任何方法可以告诉它是使用反射从A调用的? I imagine there is, but I cannot find out how. 我想有,但我不知道怎么做。

Does anyone know? 有人知道吗?

Edit: I've looked at the StackFrame class and while it seems to do what I want, it may not be reliable in production code (and I need that). 编辑:我看了看StackFrame类,虽然它似乎可以实现我想要的功能,但它在生产代码中可能并不可靠(我需要这样做)。

This is almost a duplicate of this question , but not quite . 几乎这个问题的重复,但并非完全相同 Look at that one's answers though. 看看那个人的答案。

Personally I think I'd pass in the type. 我个人认为我会通过类型。 An alternative would be to use an attribute, eg 一种替代方法是使用属性,例如

[PropertyName("Name")]
private static readonly IPageProperty NameProperty = null;

static
{
    PropertyRepository.RegisterProperties(typeof(SimpleTemplate));
}

PropertyRepostiory.RegisterProperties could then set the value of the readonly field using reflection (if this works - I haven't tried it; the readonly-ness might be enforced). 然后, PropertyRepostiory.RegisterProperties可以使用反射来设置只读字段的值(如果可行,我还没有尝试过;可能会强制执行只读性)。 It's a bit icky though... Alternatively, you could just get the property from the repository when you need it. 但是,这有点棘手...或者,您可以在需要时从存储库中获取该属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM