PowerCommands for Reflector 1.3 and Introducing Query Editor

by Jason Haley 14. October 2009 02:45

This morning I moved out the changes for PowerCommands for Reflector 1.3.  There are now 26 commands in the addin.  The two newest being ‘Open in Reflector’ and ‘Query Editor’.

Open in Reflector

image This Command shows up on the context menu of an embedded resource when the extension of the resource is either dll/exe/zip.  If the resource is a zip, then the same dialog used for OpenZip is used to show you a listing of any dll/exe files in that zip.  Once you choose an assembly then it will be extracted and added to the assembly manager tree.  If the resource is an exe or dll (not a zip), then the assembly is extracted and added to the assembly manager tree.

Query Editor

image

First off – a big thanks to Peli and Lutz for allowing me to borrow from the ‘Review’ addin … again.  Lots of good code in that addin!

Query Editor is more of a command for hard core Reflector users who really want to look at things in more detail or a different way.  Since this command is really for the advanced crowd it is disabled by default.  For awhile now I’ve wanted an addin that would allow me to ‘query’ the Reflector.CodeModel at runtime – and until lately I’ve been stuck doing this in the debugger.  If you havne’t written a Reflector addin yet, you may be wondering what I’m talking about … so here is an example:

Example query (taken from the Referenced By command mostly):

from a in AssemblyManager.Assemblies.Cast<IAssembly>()
from m in a.Modules.Cast<IModule>()
from ar in m.AssemblyReferences.Cast<IAssemblyReference>()
where ar.ToString() == ActiveItem.ToString()
select a;

What this query does is search all the assemblies loaded in the assembly manager of Reflector and look for the ones that have the AssemblyReference equal to the name of the currently selected AssemblyReference (ActiveItem).  Of course you don’t have to worry about this one because it is an already existing command, but it is a good example for the type of queries you can do. 

A few notes:

  • Only 1 query at a time is supported (currently)
  • Only C# LINQ syntax is supported
  • You’ll need to know the Reflector.CodeModel (open Reflector.exe in Reflector to see it)
  • You need to use the .Cast<T>() for collections to work in the LINQ queries
  • Use AssemblyManager.Assemblies to access all the loaded assemblies
  • Use ActiveItem when you want to go off of a selection (example: ActiveItem above requires the user to select an AssemblyReference for the query to work)
  • Use the configure button (image ) to set the Query Type – which determines the actual type of the ActiveItem required for your query.  The default is ‘Assembly Manager’ which means no Active Item is available for your queries.  The example above has a Query Type of ‘Assembly Reference’.  You can also set the Output Type to be either a text based ‘Output Window’, a ‘Linked List View’ or a Text file.
  • Linked List View is only good if the result is something that can have a Code Shortcut set
  • Output Window is good for strings
  • If you save a query, you can choose to put it on a menu or not.  The Query Type determines which menu it shows on.  For example, if the Query Type is ‘Assembly Manager’, then it shows on the Tools menu, if it is ‘Assembly Reference’, like the above example, then it would show up on the Assembly Reference context menu.

In order to get you started, I have included 10 sample queries – but you will have to extract them first by doing the following:

First enable the command if you haven’t already:

  1. Go to View menu –> Options –> Power Commands
  2. Scroll down in the list and find ‘Query Editor’ and check the box.
  3. Click OK

Extract the Sample Queries:

  1. On the Power Command Options page (#1 above), click on the ‘Extract Sample Queries’ button at the bottom right corner (above the OK/Cancel).  This will extract 10 dll’s to your “%My Documents%/Reflector/Queries” directory.
  2. Click OK

To Use the sample queries:

There should now be 5 queries on the Assembly context menu, like shown below (on a sub menu named ‘Run Query’):

image

There should also be 5 queries on the Tools menu, like shown below (also on a sub menu named ‘Run Query’):

image

To See or Edit the Sample Queries:

1.  Go to the Tools menu and click on Query Editor –> Open Query and you will get a dialog showing all the queries:

image

2.  Choose (double click or click and OK) whichever query you want to look at, and the query editor should show with the LINQ text showing for that query.

That should be enough to get you started.  I’ll write more on the parts of the Query Editor and writing your own queries soon.

Comments (6) | Post RSSRSS comment feed |

Categories:
Tags:

Comments

Comments are closed