[英]Can an extension method be added to a class property to get the value of an attribute associated with the property?
[英]how do I get the class/property/etc associated with a .NET attribute?
如果我将自定义属性应用于类,例如:
[Foo]
class Bar {}
很明显,当我检索Foo属性实例时,它与Bar关联。 在coo中的Foo实现中,我如何获得与属性实例相关联的类? 到目前为止,我所能想到的就是将其放入属性的ctor中:
[Foo(typeof(Bar)]
class Bar {}
这似乎太多余了。
在实际使用Foo时,应该使Foo类成为其属性。 因此,如果需要访问Foo中的属性,则可以将Type / instance作为参数传递。
您必须传递它。请参阅此答案...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.