Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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();}
}

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

Now, if database table altered and add a new column called cost, then I have to add another property to the class. Without adding new properties to the class is there any way do declare properties according to the table fields dynamically.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
163 views
Welcome To Ask or Share your Answers For Others

1 Answer

You could use an 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";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...