我有3個屬性的類。如何通過自定義屬性來選擇某個屬性的某些屬性
class Issuance
{
[MyAttr]
virtual public long Code1 { get; set; }
[MyAttr]
virtual public long Code2 { get; set; }
virtual public long Code3 { get; set; }
}
我需要我的自定義屬性([MyAttr]
)選擇一些在這個類的屬性。
我使用GetProperties()
但是這返回所有屬性。
var myList = new Issuance().GetType().GetProperties();
//Count of result is 3 (Code1,Code2,Code3) But count of expected is 2(Code1,Code2)
我該怎麼辦?
您需要在每個屬性上使用GetCustomAttributes並檢查返回的屬性是否屬於MyAttr類型。 – Charleh 2012-07-30 14:27:54