I have the following code which is a private method inside the form and retrieve all context menus from the form. I feel, that it is not that concise as it should be. Would be grateful for any suggestions.
private IEnumerable<ContextMenuStrip> GetContextMenus()
{
var type = this.GetType();
var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
var contextMenus = fields.Where(f => f.GetValue(this).GetType() == typeof(ContextMenuStrip));
var menus = contextMenus.Select(f=> f.GetValue(this));
return menus.Cast<ContextMenuStrip>();
}
Are you happy to include subclasses of ContextMenuStrip? If so, I’d use: