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

发布于 2024-07-10 16:39:43 字数 836 浏览 7 评论 0原文

我的反思有问题。 我需要找到实例化静态成员的类型。 我的代码如下所示:

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

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

为了成功地做到这一点,我需要跟踪所有属性以及定义它们的类型。 否则,如果定义了两个具有相同名称的属性,属性存储库将无法区分它们。

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

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

我想,解决方案将涉及反思。 有什么方法可以使用反射来确定哪种类型调用静态方法? 静态属性使用静态构造函数(编译器生成)隐式实例化。 有没有办法让我获得该构造函数的句柄? 这似乎是可行的,我只是不知道该怎么做。

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

有人知道吗?

编辑:我查看了 StackFrame 类,虽然它似乎可以实现我想要的功能,但它在生产代码中可能不可靠(我需要它)。

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.

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?

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.

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.

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡墨 2024-07-17 16:39:43

几乎这个问题,但不是完全。 不过看看那个人的答案。

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

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文