看看某个东西是否属于类类型

本文关键字:属于 类型 是否 | 更新日期: 2023-09-27 18:07:09

我正在尝试检查c#中是否有某种类型的类。它根据窗体的类型在窗体上打印某些标签。到目前为止,这就是我所拥有的,它适用于"如果"声明。但是,我得到一个错误"无法强制转换类型的对象"。在这种情况下,是否可以使用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();
    }
  }
}

看看某个东西是否属于类类型

首先,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;

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;
    }       
}
if (instance.GetType().IsClass)
{
}

应该做这个把戏。

但你真的不应该这样做,如果你需要不同的类来打印不同的东西,那么我建议你创建一个接口,并在每种类型上分别实现它。

看起来currentPerson是一个单独的实体,而不是一个集合。所以CCD_ 13或CCD_;(取决于currentPerson是否唯一(然后使用is来测试我喜欢的类型,因为它处理null检查。因此,如果没有填充任何标签,则意味着从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();
        }