0
我得到了以下情況,我有一個具有多個屬性的對象類。
此對象將不止一次用於報告目的,但並非所有屬性都是必需的,因此我正在考慮使用屬性和反射來在需要時獲得所需的屬性(用於顯示綁定目的) (而不是硬編碼使用哪些字段)。我想使用屬性和思考,以獲得以下功能帶屬性的對象反射問題
我腦子裏想的是什麼如下: - 在每個屬性中設置的顯示名稱屬性(到目前爲止好) - 設置自定義屬性(例如:useInReport1,useInReport2 ....這將是每個屬性上的布爾值)
我想知道我如何能夠實現自定義屬性[useInReport1],[useInReport2] etc .... +檢索該字段只需要
我的對象示例:
public class ReportObject
{
[DisplayName("Identity")]
[ReportUsage(Report1=true,Report2=true)]
public int ID {get {return _id;}
[DisplayName("Income (Euros)")]
[ReportUsage(Report1=true,Report2=false)]
public decimal Income {get {return _income;}
[DisplayName("Cost (Euros)")]
[ReportUsage(Report1=true,Report2=false)]
public decimal Cost {get {return _cost;}
[DisplayName("Profit (Euros)")]
[ReportUsage(Report1=true,Report2=true)]
public decimal Profit {get {return _profit;}
[DisplayName("Sales")]
[ReportUsage(Report1=false,Report2=true)]
public int NumberOfSales {get {return _salesCount;}
[DisplayName("Unique Clients")]
[ReportUsage(Report1=false,Report2=true)]
public int NumberOfDifferentClients {get {return _clientsCount;}
}
[System.AttributeUsage(AttributeTargets.Property,AllowMultiple=true)]
public class ReportUsage : Attribute
{
private bool _report1;
private bool _report2;
private bool _report3;
public bool Report1
{
get { return _report1; }
set { _report1 = value; }
}
public bool Report2
{
get { return _report2; }
set { _report2 = value; }
}
}
的改寫問:我怎樣使用自定義屬性的例子得到的屬性列表:得到這是爲了閱讀他們的價值等功能標籤爲報表= true的屬性...
好,你有多遠?你有沒有聲明屬性類? (這些是屬性,而不是屬性 - 保持術語的直觀性非常有幫助。)您是否嘗試將這些屬性應用於屬性?你有沒有試過檢查哪些屬性應用了屬性?我們不會爲你寫整個解決方案 - 請告訴你卡在哪裏。 –
問一個更好的問題是,如果他甚至困擾在該地區遠程搜索任何東西:) http://stackoverflow.com/questions/2281972/how-to-get-a-list-of-properties-with -a-given-attribute –
感謝eran otzap。看着它,它會解決我的問題:)編輯帖子,以便有屬性類也。會讓你知道這是否會解決這個問題,因爲這是我第一次處理反思。 – IanCian