因为一些业务需求,需要对WinForm窗体中的BarManager进行遍历设置。但是DevExpress.XtraBars.BarManager 没有继承Control基类,所以无法使用this.Controls进行遍历。
可以使用下面代码进行遍历:
private object SearchBarManager(){try{System.Type FormType = this.GetType();System.Reflection.FieldInfo[] fi = FormType.GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);foreach (System.Reflection.FieldInfo info in fi){if (info.FieldType == typeof(DevExpress.XtraBars.BarManager)){DevExpress.XtraBars.BarManager bar = (info.GetValue(this)) as DevExpress.XtraBars.BarManager;return bar;}}}catch (System.Exception ex){throw;}return null;}