I am running MSOCAF on my custom SharePoint 2010 solution. Its complaing about following piece of code.
for(i=0;i<Page.Control.Count;i++)
{
Control ctrl=Page.Controls[i];
if(ctrl is Button)
{
var t= ctrl as Button;
}
else if(ctrl is Label)
{
var l=ctrl as Label;
}
........
.........
if(ctrl is LinkButton)
{
var l=ctrl as LinkButton;
}
}
I need to perform this check for all control types and then do some code inside the IF block. MSOCAF reports out that its unnecessary casting inside the loop and the casting result should be cached. But as you can see its not possible as there are different control on the page. Could you please suggest some alternate to achieve the same kind of type checking and casting?