开发者

Get property name inside the attribute that declared on it

开发者 https://www.devze.com 2022-12-24 08:34 出处:网络
I 开发者_Python百科have an attribute declared on property. How can I get the property name inside the attribute?Sorry to say it but you can\'t. The attribute has no meaning of knowing to what property

I 开发者_Python百科have an attribute declared on property. How can I get the property name inside the attribute?


Sorry to say it but you can't. The attribute has no meaning of knowing to what property/class it is applied.

In general attributes alone are useless (they are just metadata decorating your classes), there must be something reading them at runtime and in order to read an attribute you already have a reference to the property this attribute is applied to, so you already know the property name.


This is backwards. The only possible way you can get the attribute value is through PropertyInfo.GetCustomAttributes(). That requires knowing the property name first so you can get the PropertyInfo object from Type.GetProperty().

0

精彩评论

暂无评论...
验证码 换一张
取 消