Retrieving context menus from the form 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&lt;ContextMenuStrip&gt; GetContextMenus()
    {
        var type = this.GetType();
        var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
        var contextMenus = fields.Where(f =&gt; f.GetValue(this).GetType() == typeof(ContextMenuStrip));
        var menus = contextMenus.Select(f=&gt; f.GetValue(this));
        return menus.Cast&lt;ContextMenuStrip&gt;();        
    }
I can't imagine why do you do this via Reflection, I believe the same can be done by walking through Controls tree. Your method will not work if context menu has property Generate member set to false in designer.

var contextMenus = fields.Where(f =&gt; f.GetValue(this).GetType() == typeof(ContextMenuStrip));
var menus = contextMenus.Select(f=&gt; f.GetValue(this));
return menus.Cast&lt;ContextMenuStrip&gt;(); 


Can be replaced with:

return fields.Select(f =&gt; f.GetValue(this)).OfType&lt;ContextMenuStrip&gt;();