我需要找到父母的控件,所以我决定递归地执行它。通过对象浏览器,我看到父的第一个控件包含我想要的控件。因此,它应该递归调用一次,然后第二次迭代的第一个控件是目标。
当我调试时,我确实看到ctrl属于ProgramHierarchy类型,但是当它到达if ... is语句时它无法进行比较......有什么想法吗? is
不能像我想的那样工作吗?
private void FindProgramHierarchyCtrl(Control parent)
{
foreach (Control ctrl in parent.Controls)
{
if (ctrl is ProgramHierarchy) // fails this even though 2nd iter of ctrl is a ProgramHierarchy
{
programHierarchyCtrl = ctrl as ProgramHierarchy;
}
else
{
if (ctrl.HasControls())
{
FindProgramHierarchyCtrl(ctrl);
}
}
}
}
编辑1
编辑2
好的,所以我更改了它以检查硬编码的控件ID,以确保我不是疯了:
foreach (Control ctrl in parent.Controls)
{
if (ctrl.ID == "programhierarchy")
{
programHierarchyCtrl = (ProgramHierarchy)ctrl;
}
else
{
if (ctrl.HasControls())
{
FindProgramHierarchyCtrl(ctrl);
}
}
}
如果最后进入If语句,但是在作业中我得到了我见过的最奇怪的例外......这里发生了什么?
[A]Stuff.Things.Web.ProgramHierarchy cannot be cast to [B]Stuff.Things.Web.ProgramHierarchy. Type A originates from 'Stuff.Things.Web, Version=1.0.5282.29772, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'C:\Stuff\Communities In Schools\Solution\Stuff.Things.Web\/bin/Stuff.Things.Web.dll'. Type B originates from 'Stuff.Things.Web, Version=1.0.5282.29772, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\Stuff.Things.web\94104cc5\724c3b9d\assembly\dl3\55541b78\a421c72d_458bcf01\Stuff.Things.Web.DLL'.