Trying to recursively find a control with if .. is [type], will not find

I need to find control over the parent, so I decided to do it recursively. Through the object browser, I see that the parent first control contains the control that I want. Therefore, it must recursively call once, and then the first element of the second iteration is the target.

When I debug, I really see that ctrl is of type ProgramHierarchy, but when it gets to the statement if .., it doesn't allow you to compare ... Any thoughts? Does not work, as I thought?

 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); } } } } 

EDIT 1

enter image description here

EDIT 2

Ok, so I changed it to verify that the control identifiers are hardcoded to make sure I'm not crazy:

 foreach (Control ctrl in parent.Controls) { if (ctrl.ID == "programhierarchy") { programHierarchyCtrl = (ProgramHierarchy)ctrl; } else { if (ctrl.HasControls()) { FindProgramHierarchyCtrl(ctrl); } } } 

And if, finally, it goes in the If statement, but when assigning, I get a strange exception that I have ever seen ... what happens here?

 [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'. 
+1
source share

Source: https://habr.com/ru/post/893394/


All Articles