C# 通过反射获取winform上的控件

比如获取Button按钮:

System.Reflection.FieldInfo[] fieldInfo = form.GetType().GetFields(System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
for (int i = 0; i < fieldInfo.Length; i++)
{
switch (fieldInfo[i].FieldType.Name)
{
case "Button":
{
Button btn= (Button)fieldInfo[i].GetValue(form);
break;
}
}
}

posted @ 2019-10-25 11:46  haishu  阅读(2059)  评论(0编辑  收藏  举报