2011-09-01 60 views
0

是否可以在不遍歷所有屬性列表的情況下讀取自定義屬性值?我使用下面的代碼來讀取屬性值attributeData.IncludeResult,但我認爲應該更容易的更優化的方式來做到這一點,而不使用foreach和迭代。獲取不帶迭代的屬性

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

回答

2

你想:

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