2012-02-29 3 views
3

属性、私は次のコードがありますちょうどリフレクションを使用しての表示/取得がクラス内

/// <remarks/> 
[System.Xml.Serialization.XmlElementAttribute("Errors", typeof(ErrorsType))] 
[System.Xml.Serialization.XmlElementAttribute("Success", typeof(SuccessType))] 
[System.Xml.Serialization.XmlElementAttribute("Warnings", typeof(WarningsType))] 
public object[] Items { 
    get { 
     return this.itemsField; 
    } 
    set { 
     this.itemsField = value; 
    } 
} 

を、それはこれらの属性を取得することは可能でしょうか? それぞれTypeに「GetCustomAttributes()」が表示されましたが、多くの喜びは得られませんでした。

答えて

4

あなたがそうのように、型自体、プロパティから属性を取得する必要はありません。(仕事にキャスト<ためSystem.Linqのをインポートすることを忘れないでください>とてToArray())

typeof(MyClass).GetProperty("Items").GetCustomAttributes(typeof(XmlElementAttribute), false); 

以上の完全な:

XmlElementAttribute[] attribs = typeof(TheType) 
        .GetProperty("Items") 
        .GetCustomAttributes(typeof(XmlElementAttribute), false) 
        .Cast<XmlElementAttribute>() 
        .ToArray(); 
関連する問題