我有一個類:動態觸及屬性的最佳做法?
public class ActionReportEntity
{
public string Caption { get; set; }
public string Description { get; set; }
public string Item1 { get; set; }
public string Item2 { get; set; }
public string Item3 { get; set; }
public string Item4 { get; set; }
public string Item5 { get; set; }
public string Item6 { get; set; }
public string Item7 { get; set; }
public string Item8 { get; set; }
public string Item9 { get; set; }
public string Item10 { get; set; }
}
我要填寫項目1項(1-10)動態,我實現這個代碼的特性:
Regex regex=new Regex("Item[0-9]+");
var props = typeof(ActionReportEntity)
.GetProperties()
.Where(p => regex.IsMatch(p.Name))
.OrderBy(p=>p.Name.Substring(4).ToInteger());
但我認爲這種方式是不是好的和強大的,因爲可能是任何一個重構這個類,並添加一些屬性突然像Item50或更改屬性。
我想我必須使用屬性,但我還沒有使用它。
什麼是實現動態屬性的最佳實踐?
更新:
提到我想在網格或報表綁定使用這個類,所以我不希望我的類的結構變化,因爲我無法列出道具綁定網格或在報告中使用它
+1「不可遍歷硬編碼的變量名」 – 2011-12-28 12:45:19
我提到我想在網格或報表綁定使用這個類,所以我不希望我的類的結構變化,因爲我無法將列表綁定綁定到網格或在報表中使用。 – 2011-12-28 12:54:22
報告無法處理IEnumerable?無論哪種方式,您總是會將IEnumerable添加到您的課程中。當你需要迭代時,你可以將所有Item#變量複製到列表中,迭代列表中的數據,然後將它們複製回來(你只需要編寫一個'copy'和'put back'算法。 ,但我確實覺得無法處理IEnumerable意味着你有更大的潛在問題... – Flater 2011-12-28 13:01:33