反射 - 获取属性的属性名称和值
我有一个类,我们将其称为“Book”,其属性名为“Name”。有了这个属性,我就有了一个与之关联的属性。
public class Book
{
[Author("AuthorName")]
public string Name
{
get; private set;
}
}
在我的主要方法中,我使用反射并希望获取每个属性的每个属性的键值对。因此,在此示例中,我希望看到“Author”作为属性名称,“AuthorName”作为属性值。
问题:如何使用反射获取属性的属性名称和值?
I have a class, lets call it Book with a property called Name. With that property, I have an attribute associated with it.
public class Book
{
[Author("AuthorName")]
public string Name
{
get; private set;
}
}
In my main method, I'm using reflection and wish to get key value pair of each attribute for each property. So in this example, I'd expect to see "Author" for attribute name and "AuthorName" for the attribute value.
Question: How do I get the attribute name and value on my properties using Reflection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
使用
typeof(Book).GetProperties()
获取PropertyInfo
实例的数组。然后对每个PropertyInfo
使用GetCustomAttributes()
来查看其中是否有任何一个具有Author
属性类型。如果是,您可以从属性信息中获取属性名称,并从属性中获取属性值。沿着这些思路扫描类型以查找具有特定属性类型的属性并返回字典中的数据(请注意,通过将类型传递到例程中可以使这变得更加动态):
Use
typeof(Book).GetProperties()
to get an array ofPropertyInfo
instances. Then useGetCustomAttributes()
on eachPropertyInfo
to see if any of them have theAuthor
Attribute type. If they do, you can get the name of the property from the property info and the attribute values from the attribute.Something along these lines to scan a type for properties that have a specific attribute type and to return data in a dictionary (note that this can be made more dynamic by passing types into the routine):
要获取字典中属性的所有属性,请使用以下命令:
如果您还想包含继承的属性,请记住从
false
更改为true
。To get all attributes of a property in a dictionary use this:
remember to change from
false
totrue
if you want to include inheritted attributes as well.如果您只需要一个特定的属性值(例如
Display
属性),您可以使用GetCustomAttribute
扩展方法:If you just want one specific attribute value – for instance, the
Display
attribute – you can use the generic version of theGetCustomAttribute
extension method:我通过编写通用扩展属性属性助手解决了类似的问题:
用法:
I have solved similar problems by writing a Generic Extension Property Attribute Helper:
Usage:
您可以使用
GetCustomAttributesData()< /code>
和
GetCustomAttributes()
:You can use
GetCustomAttributesData()
andGetCustomAttributes()
:如果您的意思是“对于采用一个参数的属性,列出属性名称和参数值”,那么在 .NET 4.5 中通过
CustomAttributeData
API 会更容易:If you mean "for attributes that take one parameter, list the attribute-names and the parameter-value", then this is easier in .NET 4.5 via the
CustomAttributeData
API:用法:
或:
Usage:
or:
虽然上面得到最多支持的答案肯定有效,但我建议在某些情况下使用稍微不同的方法。
如果您的类具有多个始终具有相同属性的属性,并且您希望将这些属性排序到字典中,则方法如下:
这仍然使用强制转换,但确保强制转换始终有效,因为您只会获得该类型的自定义属性“作者姓名”。
如果您有多个属性,上述答案将出现强制转换异常。
While the above most upvoted answers definitely work, I'd suggest using a slightly different approach in some cases.
If your class has multiple properties with always the same attribute and you want to get those attributes sorted into a dictionary, here is how:
This still uses cast but ensures that the cast will always work as you will only get the custom attributes of the type "AuthorName".
If you had multiple Attributes above answers would get a cast exception.
以下是一些可用于获取 MaxLength 或任何其他属性的静态方法。
使用静态方法...
或者在实例上使用可选的扩展方法...
或者对任何其他属性使用完整的静态方法(例如 StringLength)...
受到 Mikael Engver 答案的启发。
Here are some static methods you can use to get the MaxLength, or any other attribute.
Using the static method...
Or using the optional extension method on an instance...
Or using the full static method for any other attribute (StringLength for example)...
Inspired by the Mikael Engver's answer.
我将其写入动态方法,因为我在整个应用程序中使用了很多属性。方法:
用法:
希望这对任何人都有帮助
I wrote this into a dynamic method since I use lots of attributes throughout my application. Method:
Usage:
Hope this helps anyone
死灵术。
对于那些仍然需要维护 .NET 2.0 的人,或者那些想要在不使用 LINQ 的情况下进行维护的人:
示例用法:
或者简单地
Necromancing.
For those that still have to maintain .NET 2.0, or those that want to do it without LINQ:
Example usage:
or simply
只是寻找合适的位置来放置这段代码。
假设您有以下属性:
并且您想要获取 ShortName 值。您可以这样做:
或者使其通用:
Just looking for the right place to put this piece of code.
let's say you have the following property:
And you want to get the ShortName value. You can do:
Or to make it general:
我想在 Adam Markowitz 提供的答案的基础上添加一些内容
我们可以使用以下代码根据我们使用的自定义属性类型直接过滤掉任何属性。
I would like to add something on top of the answer provided by Adam Markowitz
We can use the following code to directly filter out any property based on the custom attribute type we used.
在此示例中,我使用 DisplayName 而不是 Author,因为它有一个名为“DisplayName”的字段,要显示一个值。
In this example I used DisplayName instead of Author because it has a field named 'DisplayName' to be shown with a value.
要从枚举获取属性,我正在使用:
// 使用
to get attribute from enum, i'm using :
// Using
如果您想获取具有自定义属性的属性,请尝试以下操作:
If you want get property having the custom Attribute then please try the following: