I have a situation in WebForm where I need to reload the control tree to find all the controls that implement this interface.
How can I do it?
I tried writing an extension method like this
public static class ControlExtensions
{
public static List<T> FindControlsByInterface<T>(this Control control)
{
List<T> retval = new List<T>();
if (control.GetType() == typeof(T))
retval.Add((T)control);
foreach (Control c in control.Controls)
{
retval.AddRange(c.FindControlsByInterface<T>());
}
return retval;
}
}
But this does not like the cast to Ton line 7. I also thought about trying the as operator, but this does not work with interfaces.
I saw the Scott Hanselmans investigation , but I couldn’t extract anything useful from it.
Can anyone give me any directions. Thanks.
Greg
source
share