在我的类中,我有私有变量和属性.
private string _itemCOde=string.Empty; private string _itemName=string.Empty; public string ItemCode { get { return _itemCode; } set { _itemCode = value == null ? value : value.Trim();} } public string ItemName { get { return _itemName; } set { _itemName = value == null ? value : value.Trim();} }
现在,如果数据库表被更改并添加了一个名为cost的列,那么我必须向该类添加另一个属性.没有向类添加新属性有没有办法根据表字段动态地声明属性.
解决方法
您可以使用
ExpandoObject
:
Represents an object whose members can
be dynamically added and removed at
run time.
dynamic expando = new ExpandoObject(); expando.Cost= 42.0; expando.ItemName = "Shoes";