0

我正在尝试检查某些东西是否是 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

4 回答 4

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;
于 2013-04-03T12:54:50.710 回答
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;
    }       
}
于 2013-04-03T12:53:13.513 回答
0
if (instance.GetType().IsClass)
{

}

应该做的伎俩。

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

于 2013-04-03T12:53:12.200 回答
0

看起来currentPerson是单个实体而不是集合。so SingleorFirstOrDefault也可以;(取决于 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();
        }
于 2013-04-03T12:59:13.643 回答