Generics, Casting and Anonymous Delegates and the Where clause
I have a situation where the code looks like:
( ( TextBox ) ( _someParentControl.FindControl( “_childControlId” ) ) ).Text = “SomeText”;
( ( TextBox ) ( _someParentControl.FindControl( “_childControlId” ) ) ).Enabled = someBool;
repeat about 10 times with different child control type, parent controls, child Ids, and properties.
obviously, there’s a similarity and refactoring was in order.
private static void ExecuteOnControl
{
T control = parent.FindControl( id ) as T;
if (control != null && action != null)
{
action( control );
}
}
The where clause is important because you can’t cast “As” within the function without having some limits on the generc T.
Which allowed me to shrink the example down to:
ExecuteOnControl
This can be further simplified since the delegate can be declared once and used later.
Action
ExecuteOnControl
ExecuteOnControl
One can argue here that the new code isn’t really that much more readable though, but it was good hacking
Discussions