看看是否有类型的东西

时间:2013-04-03 12:50:14

标签: c# class types

我试图检查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();
    }
  }
}

4 个答案:

答案 0 :(得分:7)

首先,currentPersion是项目的集合。使用Single方法将其设为一个项目:

var currentPerson =
  people.person.Where(p => p.Forename + " " + p.Surname == pName).Single();

(如果您可能根本没有匹配,则使用SingleOrDefault,然后检查currentPersion是否为空。)

其次,在尝试投射后,您正在检查currentPerson变量而不是accademic变量:

AccademicStaff accademic = currentPerson as AccademicStaff;
if (accademic != null)

现在你也不需要循环。使用第一部分中的accademic变量,并在第二部分中将引用转换为AdminStaff

AdminStaff admin = currentPerson as AdminStaff;

答案 1 :(得分:2)

if ... else ...位移到foreach循环中,因为where选择器返回的是一个集合,而不是一个人。

var currentPerson = people.person.Where(p => p.Forename + " " + p.Surname == pName);

foreach (var person in currentPerson)
{

    AccademicStaff accStaff = person as AccademicStaff;
    if (accStaff != null)
    {
        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
        label9.Text = person.Forename + " " + person.Surname;
        label10.Text = person.IdentificationNumber.ToString();
        label11.Text = person.DateOfBirth;
        label12.Text = person.Address;
        label13.Text = person.Office;
        label6.Text = "Job Role";
        label14.Text = person.JobRole;
    }       
}

我删除了标签上Dispose()的来电,没有任何意义。

编辑你可能会有点像这样缩短它:

var currentPerson = people.person.Where(p => p.Forename + " " + p.Surname == pName);

foreach (var person in currentPerson)
{

    label9.Text = person.Forename + " " + person.Surname;
    label10.Text = person.IdentificationNumber.ToString();
    label11.Text = person.DateOfBirth;
    label12.Text = person.Address;
    label13.Text = person.Office;

    AccademicStaff accStaff = person as AccademicStaff;
    if (accStaff != null)
    {
        label14.Text = accStaff.School;
        label15.Text = accStaff.ModuleLeaderOf;
        label16.Text = accStaff.ProgramLeaderOf;
    }
    else
    {
        label6.Text = "Job Role";
        label14.Text = person.JobRole;
    }       
}

答案 2 :(得分:0)

if (instance.GetType().IsClass)
{

}

应该做的伎俩。

但你不应该这样做,如果你需要不同的类来打印不同的东西,那么我建议你去创建一个界面并在每种类型上单独实现它。

答案 3 :(得分:0)

看起来currentPerson是单个实体而不是集合。所以SingleFirstOrDefault也会起作用; (取决于currentPerson是否唯一)然后使用is测试我喜欢的类型,因为它处理空检查。因此,如果没有填充任何标签,则意味着从linq语句返回的人都不是类型。

    people.CreatePeople();
    var currentPerson = people.person.Where(p => p.Forename + " " + p.Surname == pName).Single();

        if(currentPerson is AcademicStaff)
        {
            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 if(currentPerson is AdminStaff)
        {
            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();
        }