winform 查找控件并获取特定类型控件

时间:2020-12-31 18:29:58
            //通过反射获取所有控件集合
System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
for (int i = ; i < fieldInfo.Length; i++)
{
//获取特定控件类型
if (fieldInfo[i].FieldType.Name != "ContextMenuStrip") continue;
//通过名称获取控件
ContextMenuStrip cms = (ContextMenuStrip)fieldInfo[i].GetValue(this); }

感谢博主提供的思路https://blog.csdn.net/softimite_zifeng/article/details/54289012