让你的所有类:汽车,人从基类MyBaseClass继承并将你的列表转换为该类型/类
public List<MyBaseClass> Objects = new List<MyBaseClass>();
protected void Page_Load(object sender, EventArgs e)
{
Objects = (List<MyBaseClass>) HttpContext.Current.Items["Objects"];
}
然后你可以迭代你的 List 并确定确切的类型
foreach(MyBaseClass mbc in Objects)
{
if(mbc is People)
{
//its a People object
}
else if(mbc is Car)
{
//ist a Car object
}
}
请记住,在您的代码中,当您添加或创建对象列表时,您需要将其声明为
List<MyBaseClass> Objects
顺便说一句,您当前的列表似乎是人员列表,所以这应该可以
public List<People> Objects = new List<People>();
protected void Page_Load(object sender, EventArgs e)
{
Objects = (List<People>) HttpContext.Current.Items["Objects"];
}