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

时间:2022-11-13 17:03:57

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). It keeps track of all the properties that have been registered using the type system that I'm building.

PropertyRepository是一个属性的存储库(显然)。它跟踪使用我正在构建的类型系统注册的所有属性。

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. How can I do that?

所以,我想要做的是找出定义NameProperty的类型并存储类型和名称。我怎样才能做到这一点?

I want to use strong typing, i.e. I do not want to send the type as an argument to PropertyRepository.Register. That would be error-prone since I can't validate that the type argument is correct.

我想使用强类型,即我不想将类型作为参数发送到PropertyRepository.Register。这很容易出错,因为我无法验证类型参数是否正确。

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? I imagine there is, but I cannot find out how.

换句话说:如果方法A调用方法B,那么B有什么方法可以告诉它是使用反射从A调用的吗?我想有,但我不知道如何。

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类,虽然它似乎做了我想要的,但它在生产代码中可能不可靠(我需要它)。

1 个解决方案

#1


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, e.g.

我个人认为我会传递这种类型。另一种方法是使用属性,例如

[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). It's a bit icky though... Alternatively, you could just get the property from the repository when you need it.

PropertyRepostiory.RegisterProperties然后可以使用反射设置readonly字段的值(如果这有效 - 我没有尝试过;可能会强制执行readonly-ness)。虽然它有点icky ...或者,您可以在需要时从存储库中获取属性。

#1


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, e.g.

我个人认为我会传递这种类型。另一种方法是使用属性,例如

[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). It's a bit icky though... Alternatively, you could just get the property from the repository when you need it.

PropertyRepostiory.RegisterProperties然后可以使用反射设置readonly字段的值(如果这有效 - 我没有尝试过;可能会强制执行readonly-ness)。虽然它有点icky ...或者,您可以在需要时从存储库中获取属性。