The if
and switch
operations are not bad. Even performing some rudimentary type checks is not necessarily bad, especially when the types used cannot be used polymorphically. When this logic is expressed several times, it is something that is frowned upon because you are repeating yourself and you have several service points for the same changes.
In the source code you are executing
var blah = obj as Foo; if (blah != null) { someList.Add(blah.Value); }
And repeat this for several types of controls. But then in your personal method later, you have basically the same logic, expressed as many times.
var blah = obj as Foo; if (blah != null) return blah.Value;
The only difference is that in the first fragment you take a value and add it to the list. In the second, you return the value. The first fragment should do away with the type checking logic; it should use the logic already expressed in another method.
foreach (var control in childControls) { listOfValues.Add(GetControlValue(control)); }
The idea is not repeated. DRY.
source share