我正在尝试检查某些东西是否是 c# 中的某种类型的类。它根据表单的类型在表单上打印出某些标签。这就是我到目前为止所拥有的,它适用于“if”语句。但是,我收到“无法转换类型的对象”的错误。在这种情况下是否可以使用 if-else 语句?
public void ShowStaffData(string pName)
{
//Gets Staff Details from the name slected int he list box in the form
people.CreatePeople();
var currentPerson =
people.person.Where(p => p.Forename + " " + p.Surname == pName);
// How the info is printed out if person selected in
// a member of Accademic Staff
AccademicStaff accademic = currentPerson as AccademicStaff;
if (currentPerson!=null)
{
foreach (AccademicStaff accStaff in currentPerson)
{
label9.Text = accStaff.Forename + " " + accStaff.Surname;
label10.Text = accStaff.IdentificationNumber.ToString();
label11.Text = accStaff.DateOfBirth;
label12.Text = accStaff.Address;
label13.Text = accStaff.Office;
label14.Text = accStaff.School;
label15.Text = accStaff.ModuleLeaderOf;
label16.Text = accStaff.ProgramLeaderOf;
}
}
else
{
// How the info is printed out if person selected in
// a member of Admin Staff
foreach (AdminStaff admin in currentPerson)
{
label9.Text = admin.Forename + " " + admin.Surname;
label10.Text = admin.IdentificationNumber.ToString();
label11.Text = admin.DateOfBirth;
label12.Text = admin.Address;
label13.Text = admin.Office;
label6.Text = "Job Role";
label14.Text = admin.JobRole;
label7.Dispose();
label8.Dispose();
label15.Dispose();
label16.Dispose();
}
}
}