I’m trying to convert the following code to a GENERIC function, by eliminating the need to hardcode my ‘control comparison’. any way to achieve this?:)
private Control getControlByAccessible(string accessDesc, string accessName, Control wrapper)
{
foreach (Control ctrl in wrapper.Controls)
{
if (ctrl is TextBox)
if (ctrl.AccessibleDescription == accessDesc && ctrl.AccessibleName == accessName)
return ctrl;
}
return null;
}
This was my first attempt, wasn’t yield any result so far.
private Control getControlByAccessible(string accessDesc, string accessName, Control wrapper, Type aControlType)
{
foreach (Control ctrl in wrapper.Controls)
{
if (ctrl is aControlType)
if (ctrl.AccessibleDescription == accessDesc && ctrl.AccessibleName == accessName)
return ctrl;
}
return null;
}
You can use
Type.IsInstanceOfType:However, I would personally make it a genuinely generic method with a type parameter if possible, then use LINQ’s
OfTypemethod. I’d also probably make it an extension method:Then call it like this: