【问题标题】:how do I get the class/property/etc associated with a .NET attribute?如何获取与 .NET 属性关联的类/属性/等?
【发布时间】:2010-03-07 07:13:49
【问题描述】:

如果我将自定义属性应用于类,例如:

[Foo]
class Bar {}

很明显,当我检索我的 Foo 属性实例时,它与 Bar 相关联。在 Foo 实现中,比如在 ctor 中,我如何获取与属性实例关联的类?到目前为止,我所能想到的只是将它放入属性的 ctor 中:

[Foo(typeof(Bar)]
class Bar {}

这似乎是多余的。

【问题讨论】:

    标签: c# .net class attributes


    【解决方案1】:

    当你实际使用 Foo 时,你应该让类 Foo 是一个属性。因此,如果您需要访问 Foo 中的属性,您可以将类型/实例作为参数传入。

    【讨论】:

      【解决方案2】:

      你必须把它传递进去。看到这个答案......

      How do I get the member to which my custom attribute was applied?

      【讨论】:

        猜你喜欢
        • 2020-08-21
        • 1970-01-01
        • 1970-01-01
        • 2021-10-11
        • 1970-01-01
        • 2010-10-02
        • 1970-01-01
        • 1970-01-01
        • 2016-01-26
        相关资源
        最近更新 更多