Find Components Non Visual C#

后端 未结 1 1612
长情又很酷
长情又很酷 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<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>();
    }
    
    0 讨论(0)
提交回复
热议问题