c#确定列表项的对象类型
本文关键字:对象 类型 列表 | 更新日期: 2023-09-27 18:14:54
我正试图为我的c#类做一个项目,将学生对象(父)或DormStudent(子)中的学生添加到为学生对象设置的列表中。我需要读取一个基于学生ID的对象,并确定它是学生还是宿舍学生,并相应地填写表单的其余部分。
int pos = MainMenu.myList.FindIndex(x => x.ID == validID);
if (MainMenu.myList[pos] == Student)
{
Student tempStu = MainMenu.myList[pos];
nameTextBox.Text = tempStu.Name;
}
else
{
DormStudent tempDorm = MainMenu.myList[pos];
dormCheckBox.Checked = true;
dormTextBox.Text = tempDorm.Dorm;
if (tempDorm.MealType == "B")
{
basicRadioButton.Checked = true;
}
else if (tempDorm.MealType == "M")
{
mediumRadioButton.Checked = true;
}
else
{
highRadioButton.Checked = true;
}
}
这里是列表和对象项
public static List<Student> myList = new List<Student>();
[Serializable]
public class DormStudent : Student
{
public string Dorm{get; set;}
public string MealType { get; set; }
public DormStudent() : base()
{
Dorm = "No Dorm";
MealType = "B";
}
public DormStudent(int i, string n, string d, string m) : base(i, n)
{
Dorm = d;
MealType = m;
}
}
[Serializable]
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public List<int> Grades;
public Student()
{
ID = 0;
Name = "No Student";
Grades = new List<int>();
}
public Student(int i, string n)
{
ID = i;
Name = n;
}
}
由于DormStudend派生自student,因此您需要首先询问该对象是否为DormStudent。(通常从具体到一般的做法)
所以你需要像这样交换if语句:
if(MainMenu.myList[pos] is DormStudent)
{
...
}
else
{
...
}
回复你的评论:您可以使用as关键字来代替,使它更容易一些。它将返回被强制转换的对象,如果不能强制转换,则返回null。
Student student = MainMenu.myList[pos];
DormStudent dormStudent = student as DormStudent;
if(dormStudent!= null)
{
dormTextBox.Text = dormStudent.Dorm;
}
else
{
nameTextBox.Text = student.Name;
}
对于这种设计是否对其他人来说是最佳的,我留下了评论,但是您要寻找的是is
操作符关键字。
var someStudent = MainMenu.myList[pos];
//Check for null here
if (someStudent is DormStudent )
{
DormStudent tempDorm = someStudent as DormStudent ;
dormCheckBox.Checked = true;
dormTextBox.Text = tempDorm.Dorm;
if (tempDorm.MealType == "B")
{
basicRadioButton.Checked = true;
}
else if (tempDorm.MealType == "M")
{
mediumRadioButton.Checked = true;
}
else
{
highRadioButton.Checked = true;
}
Student tempStu = someStudent ;
nameTextBox.Text = tempStu.Name;
}
else
{
nameTextBox.Text = someStudent.Name;
}
但是你也可以用null检查as
:
var someStudent = MainMenu.myList[pos];
//Null check here?
var dormStudent = someStudent as DormStudent;
if (dormStudent != null)
{
DormStudent tempDorm = someStudent as DormStudent ;
dormCheckBox.Checked = true;
dormTextBox.Text = dormStudent.Dorm;
if (dormStudent.MealType == "B")
{
basicRadioButton.Checked = true;
}
else if (dormStudent.MealType == "M")
{
mediumRadioButton.Checked = true;
}
else
{
highRadioButton.Checked = true;
}
}
nameTextBox.Text = someStudent.Name;
if (!myList[pos] is DormStudent))
需要is操作符来测试类型和后代。您也可以从列表中检索Student并使用作为操作符将其强制转换为DormStudent,并检查是否为空。
使用is操作符判断对象是否为特定类型。
。
if (MainMenu.myList[pos] is Student)
{
...
}
else if (MainMenu.myList[pos] is DormStudent)
{
...
}
现在,在这个特殊的情况下,这将不起作用,因为我上面编写的代码将在第一个子句中捕获两种类型,因为DormStudent
继承自Student
。
要处理此问题,请反转检查:
if (MainMenu.myList[pos] is DormStudent)
{
...
}
else if (MainMenu.myList[pos] is Student)
{
...
}
这仍然有一个问题,如果继承DormStudent
或Student
的任何其他类型出现,它将被上面的if语句捕获。如果您不希望这样做,下面是如何仅识别已知类型的方法:
if (MainMenu.myList[pos].GetType() == typeof(DormStudent))
{
...
}
else if (MainMenu.myList[pos].GetType() == typeof(Student))
{
...
}
else
{
... // other type?
}