背景...
假设我有一个名为cars的类。我们只需要存储汽车名称和ID。 还假设我有一个基于admin类的管理页面,我在一个名为totalCars的int中设置要创建的汽车总数。
问题: 如何动态地创建汽车作为可以从代码中任何位置访问的字段,同时根据totalCars中的数字创建汽车的总数?
示例代码:
Cars car1 = new Cars();
int totalCars;
//Somehow I want to create cars objects/fields based on the
//number in the totalCars int
protected void Page_Load(object sender, EventArgs e)
{
car1.Name = "Chevy";
car1.ID = 1;
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = car1.Name.ToString();
//this is just a sample action.
}
List<Car>
呢?如果生成字段的代码是动态的,那么其他代码如何期望这些字段存在呢? - Lukazoid