Tomas
Tomas

Reputation: 18117

Get attribute without iteration

Is it possible to read custom attribute value without iterating through all list of attributes? I use code below to read attribute value attributeData.IncludeResult but I think should easy more optimal way to do that without using foreach and iteration.

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

Upvotes: 0

Views: 134

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063383

You want:

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

Upvotes: 2

Related Questions