我有一个类:
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或更改某个属性。
我认为我必须使用属性,但我还没有使用它。
如何实现动态属性的最佳实践?
更新:
提到我想在网格或报告中绑定此类,所以我不想改变我的类结构,因为我无法将列表属性绑定到网格或在报告中使用它