将自定义属性添加到类

问题描述:

//在我的课堂上,我有像这样的私有变量和属性.

//In my class I have private variables and properties like this.

私有字符串_itemCOde = string.Empty;
私人字串_itemName = string.Empty;

private string _itemCOde=string.Empty;
private string  _itemName=string.Empty;

公共字符串ItemCode
       {
          得到{return _itemCode; }
          设置{_itemCode = value == null? value:value.Trim();}
       }

public string ItemCode
        {
            get { return _itemCode; }
            set { _itemCode = value == null ? value : value.Trim();}
        }

公共字符串ItemName
       {
          得到{return _itemName; }
          设置{_itemName = value == null? value:value.Trim();}
       }

public string ItemName
        {
            get { return _itemName; }
            set { _itemName = value == null ? value : value.Trim();}
        }

根据该属性,我从sql表中选择数据后创建了Item对象.

According to this properties I create Item objects after selecting the data from the sql table.

现在,如果数据库表已更改并添加了一个名为cost的新列,那么我必须向该类中添加另一个属性.无需执行此操作,就无法根据数据库表添加属性.

Now, if database table altered and add a new column called cost, then I have to add another property to the class. Without doing this is there any way to add property according to the database table.

 

 

从技术上讲,是的,可以通过在运行时动态编译类来实现,但是我认为这会给您带来更多的影响心痛重于其价值.

Technically, yes its possible by dynamically compiling a class at runtime but I think this will cause you far more heartache than its worth.

只需将多余的属性添加到类中即可.为什么不呢?

Just add the extra property to the class. Why would you not?