且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找Windows窗体C#组件(未控制)

更新时间:2023-12-06 11:06:52

令人惊讶,似乎要做到这一点是通过反射的唯一途径。

Surprisingly, it seems the only way to do this is via reflection.

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