Is there an easier way to typecast with unknown types?

Posted by Adam S on Stack Overflow See other posts from Stack Overflow or by Adam S
Published on 2010-05-24T20:13:55Z Indexed on 2010/05/24 20:51 UTC
Read the original article Hit count: 291

Filed under:
|
|
|

Hi all. I am writing a function to recurse my XAML and add all the controls to a hashtable, with their names being the keys. Unfortunately it seems like I have to go through and list every possible type:

void Recurse_Controls(object start)
{
    string start_type = start.GetType().ToString();

    if (start_type == "StackPanel")
    {
        ControlsByName.Add(((StackPanel)start).Name, start);
        foreach (object item in ((StackPanel)start).Children)
        {
            Recurse_Controls(item);
        }
    }

    if (start_type == "Grid")
    {
        ControlsByName.Add(((Grid)start).Name, start);
        foreach (object item in ((Grid)start).Children)
        {
            Recurse_Controls(item);
        }
    }
}

Is there a simpler way of doing this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf