Find Components Non Visual C#

后端 未结 1 1615
长情又很酷
长情又很酷 2021-01-22 11:21

I\'ve tried numerous methods to list all non-visual components of a form such as OpenDialog, ImageList, TableAdapters, etc and could not find anything. To find the on-screen con

1条回答
  •  面向向阳花
    2021-01-22 11:51

    private IEnumerable 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();
    }
    

    0 讨论(0)
提交回复
热议问题