查找非 Visual C# 的组件 [英] Find Components Non Visual C#

查看:33
本文介绍了查找非 Visual C# 的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了多种方法来列出表单的所有非可视组件,例如 OpenDialog、ImageList、TableAdapters 等,但找不到任何内容.为了找到屏幕上的控件,我在控件"屏幕中使用 Foreach 进行了管理,但对于那些非可视组件,我一无所获.我使用了下面的代码但没有成功:

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 controls, I managed using Foreach in "Controls" screen but for those non-visual components I found nothing. I used the code below but without success:

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 solve this issue?

推荐答案

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

这篇关于查找非 Visual C# 的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆