【问题标题】:See if something is of type class看看有没有什么是类型类
【发布时间】:2013-03-25 03:32:16
【问题描述】:

我正在尝试检查某些东西是否是 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 运算符有帮助?

标签: c# class types


【解决方案1】:

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

【讨论】:

  • 这就是为什么这个家伙的代表是 220K+
  • 仅当 pName 是唯一的时才使用 Single()。如果可能有多个同名的人,那么如果您想要一条记录,请不要使用 Single() 并使用 foreach 处理每条记录(或使用 FirstOrDefault() 并检查是否为空)。如果 pName 是唯一的,但可能与记录不匹配,请使用 SingleOrDefault() 而不是 Single()
  • 你也可以检查类型: if (currentPerson.GetType() == typeof(AccademicStaff)) { ... } else { ... }
  • @AndyNichols:我假设代码的制作方式不能超过一个匹配项。如果有,您将需要一种完全不同的方式来显示结果。循环结果并将它们放在相同的标签中是没有意义的。
  • @AndyNichols:我已经在答案中添加了关于SingleOrDefault 的注释,甚至在我看到你的第一条评论之前。 :)
【解决方案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;
    }       
}

【讨论】:

    【解决方案3】:
    if (instance.GetType().IsClass)
    {
    
    }
    

    应该做的伎俩。

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

    【讨论】:

      【解决方案4】:

      看起来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();
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多