List non-visual component

4

I've tried several ways to list all the non-visual components of a form, such as: OpenDialog , ImageList , TableAdapters , and so on. but without success.

To find the controls on the screen, I got a Foreach in the Controls from the screen, but for those non-visual components, I found nothing.

My current code:

private IEnumerable<Component> EnumerateComponents()
{
    return from field in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
           where typeof (Component).IsAssignableFrom(field.FieldType)
           let component = (Component) field.GetValue(this)
           where component != null
           select component;
}

Any idea how to do this non-visual component listing process?

    
asked by anonymous 19.11.2014 / 21:15

1 answer

1

As posted in the author's own comments, the code below lists all non-visual components of a form.

private IEnumerable<Component> EnumerateComponents()
{
    return this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
            .Where(f => typeof(Component).IsAssignableFrom(f.FieldType))
            .Where(f => !typeof(Control).IsAssignableFrom(f.FieldType))
            .Select(f => f.GetValue(this))
            .OfType<Component>();
}

Originally in Find Components Non Visual C # .

    
23.05.2017 / 14:37