Query Editor: Making Queries Available on Menus

by Jason Haley 7. November 2009 04:47

In the last entry Getting Started with Query Editor, I introduced using the Query Editor (part of the 1.3 release of PowerCommands for Reflector).  In this entry I’ll cover how to add your queries to Reflector’s menu system.

In PowerCommands for Reflector 1.3 and Introducing Query Editor, I mentioned the sample queries are available on the ‘Run Query’ menus (on either the Tools menu or the Assembly context menu) – this functionality is also available to you when you write your own queries. 

Example – List Events query for a Selected Type

In order to show how to add a query to the menu system in Reflector, let’s write a query that may be useful for you in the process.  Say you are looking at the System.Windows.Forms.Control type in Reflector and are only interested in looking at the events.  If you aren’t familiar with this type, it has How can you quickly get a filtered list showing only the events on that type?  The simple answer is to create your own query and add it to the Run Query context menu.

In this case, we know we want to use the selected type – so the Query Type will need to be ‘Type’, therefore our query can use ActiveItem as an ITypeDeclaration.

image
Figure 1

Figure 1 shows the ITypeDeclaration type interface.  ITypeDeclaration has a property named Events which is a collection of IEventDeclaration objects.  Since we want our results to be the list of each event in the type, we will need to enumerate this Events collection.

To enumerate the Events collection and return an IEventDeclaration (which is one of the types the Linked ListView works with) we will need a query like this:

from e in ActiveItem.Events.Cast<IEventDeclaration>()
select e
 
Now let’s create and save this query to test it on the menu system.
 
1.  Open Query Editor (Tools –> Query Editor –> Display Query Editor or New Query)

2.  Type the following the query editor for the query text:

from e in ActiveItem.Events.Cast<IEventDeclaration>()
select e
3.  Click the Save button (image ) and the save dialog will show.  Make sure all the values are entered like shown in the dialog below (except the Location will differ of course):
image
Figure 2

4.  Click Save

Now let’s test it.  Find the System.Windows.Forms.Control type (or you can click on the link for Reflector to find it for you) and right click on a type name in the assembly list.  You should now have a Run Query submenu on the context menu with a ‘List Events’ item (like shown below).

image
Figure 3

If you click on the List Events menu item, the query will execute and you should get a listing of only the events on that type, like shown in Figure 4 below.

image
Figure 4

If you wanted to create similar queries for fields, methods and properties you could use queries like the following:

List Fields query

from f in ActiveItem.Fields.Cast<IFieldDeclaration>()
select f

List Methods query

from m in ActiveItem.Methods.Cast<IMethodDeclaration>()
select m

List Properties query

from p in ActiveItem.Properties.Cast<IPropertyDeclaration>()
select p

A little about how the query is added to the menu

When the ‘Show on Menu?’ checkbox is checked on the save dialog, the query will be added to only one menu.  The menu is determined by the Query Type.  The table below shows the Query Type to Menu .mappings

Query Type Node Image Menu/Context Menu
Assembly Manager   Tools
Assembly image Browser.Assembly
Assembly Reference image Browser.AssemblyReference
Module Reference image Browser.ModuleReference
Module image Browser.Module
Resource image Browser.Resource
Type image Browser.TypeDeclaration
Field image Browser.FieldDeclaration
Method image Browser.MethodDeclaration
Event image Browser.EventDeclaration
Property image Browser.PropertyDeclaration

NOTE: Since the context menu names may not mean much to you – I added the Node Image column to help you understand which nodes in the assembly tree provide the corresponding context menus.

Summary

This entry walked through an sample query and showed to get that query to show up on Reflector’s menu system (by checking the Show on Menu checkbox).  I also provided the complete Query Type to Menu mapping for your future reference.

In the next entry, I’ll walk through some examples of queries that look at the metadata of types to solve some interface related problems.

Comments (1) | Post RSSRSS comment feed |

Categories:
Tags:

Comments

Comments are closed