将自定义属性添加到类 [英] Add custom properties to a class

查看:102
本文介绍了将自定义属性添加到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

//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?


这篇关于将自定义属性添加到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆