C#确定列表项的对象类型

时间:2014-07-26 19:13:47

标签: c#

我正在尝试为我的C#类做一个项目,该项目将Student对象(父对象)或DormStudent(子对象)的学生添加到为Student对象设置的List中。我需要根据学生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;
        }
    }

4 个答案:

答案 0 :(得分:1)

要确定对象是否属于特定类型,请使用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)
{
    ...
}

这仍然存在这样的问题:如果继承自DormStudentStudent的任何其他类型,它将被上述if语句捕获。如果您不想这样,以下是如何识别已知类型:

if (MainMenu.myList[pos].GetType() == typeof(DormStudent))
{
    ...
}
else if (MainMenu.myList[pos].GetType() == typeof(Student))
{
    ...
}
else
{
    ... // other type?
}

答案 1 :(得分:1)

由于DormStudend源自学生,因此您需要首先询问该对象是否为DormStudent。 (通常从具体到一般的方法)

所以你需要像这样交换你的If语句:

if(MainMenu.myList[pos] is DormStudent)
{
  ...
}
else
{
   ...
}

回复你的评论: 您可以使用as关键字来简化它。 因为基本上是一个&#39;尝试演员&#39;它将返回已转换的对象,如果无法转换,则返回null。

Student student = MainMenu.myList[pos];
DormStudent dormStudent = student as DormStudent;

if(dormStudent!= null)
{
     dormTextBox.Text = dormStudent.Dorm;
}
else
{
    nameTextBox.Text = student.Name;
}

答案 2 :(得分:1)

我将评论此设计是否最适合其他人,但您要查找的是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;
}

但你也可以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;

答案 3 :(得分:1)

if(!(MainMenu.myList [pos]是DormStudent))

您需要运算符来测试后代的类型和测试。您也可以从列表中检索学生并使用 as 运算符将其投射到DormStudent并检查是否为null。