我希望通过循环遍历它们并使用如下反射来获取MDI表单值的每个ToolStripMenuItem:
FieldInfo[] menuitems = GetType().GetFields(BindingFlags.GetField |
BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var item in menuitems )
if (item.FieldType.Equals(typeof(ToolStripMenuItem)))
MessageBox.Show(
item.FieldType.GetProperty("Tag").GetValue(item, null).ToString());
Run Code Online (Sandbox Code Playgroud)
但我得到"对象不匹配目标类型"错误,我很困惑,不知道指定哪个对象作为获取值的源对象.
请指导我...提前谢谢你.
这不是反思的情况.
要获取菜单项,您应首先获得对ToolStrip的引用,然后从中对其Controls集合进行迭代.
代码看起来像这样:
foreach(Control ctrl in _myToolStrip.Controls)
{
MessageBox.Show(ctrl.Tag);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
367 次 |
| 最近记录: |