我想檢查是否有某種類型的類在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();
}
}
}
你得到的錯誤是哪一行? –
也許['is'](http://msdn.microsoft.com/en-us/library/scekt9xw%28v=vs.71%29.aspx)運算符有幫助嗎? – phg