不迭代获取属性

本文关键字:属性 获取 迭代 | 更新日期: 2023-09-27 18:07:10

是否可以读取自定义属性值而不遍历所有属性列表?我使用下面的代码来读取属性值attributeData.IncludeResult,但我认为应该更容易实现更优的方法,而不使用foreach和迭代。

foreach (var customAttributeData in
         propertyInfo.GetCustomAttributes(typeof(WebClientAttribute), false))
{
    var attributeData = (WebClientAttribute)customAttributeData;
    myData = attributeData.IncludeResult
}

不迭代获取属性

你想:

WebClientAttribute attrib = (WebClientAttribute)
    Attribute.GetCustomAttribute(propertyInfo, typeof(WebClientAttribute));