A trick to jump directly to a Cmdlet’s implementation in Reflector

by oising 30. October 2008 18:44

Fellow PowerShell MVP and developer all-round expert administrator Brandon Shell asked me how he could navigate to a Cmdlet’s implementation in Lutz Roeder’s Reflector -- which has now been acquired by Red-Gate Software btw. At first I was going to explain to him how snap-ins work and how to figure out where things are, and then a really simple trick hit me that uses some fairly secret command-line parameter that Reflector can accept, namely the /select parameter:

  1. function Reflect-Cmdlet {  
  2.     param([Management.Automation.CommandInfo]$command)  
  3.     if ($input) {  
  4.         trap { $_; break }  
  5.         $command = $input | select -first 1  
  6.     }      
  7.          
  8.     # resolve to command if this is an alias  
  9.     while ($command.CommandType -eq "Alias") {  
  10.         $command = Get-Command ($command.definition)  
  11.     }  
  12.       
  13.     $name = $command.ImplementingType      
  14.     $DLL = $command.DLL  
  15.       
  16.     if (-not (gcm reflector.exe -ea silentlycontinue)) {  
  17.         throw "I can't find Reflector.exe in your path." 
  18.     }  
  19.        
  20.     reflector /select:$name $DLL 

Just pipe the output of get-command to it, like: gcm dir | reflect-cmdlet and Reflector will open up with the class selected (it takes a few seconds).

Update: Doug pointed out in a comment that the gcm reflector.exe line could benefit from an erroraction to keep it silent on failure, so only the throw message shows. Thanks Doug!

Tags:

.NET | Cmdlets | PowerShell | PowerShell 2.0

Comments (3) -

Doug
Doug
10/31/2008 2:12:54 AM #

Super idea.

I added -ErrorAction SilentlyContinue to Get-Command so only the throw message displays.

if (-not (gcm reflector.exe -ErrorAction SilentlyContinue)) {  
   throw "I can't find Reflector.exe in your path."  
}  

Reply

halr9000
halr9000
11/2/2008 4:13:41 PM #

Brilliant!  Adding this to my toolkit with extreme prejudice.

Reply

halr9000
halr9000
11/2/2008 4:16:35 PM #

And if you have PSCX you can use the apv cmdlet like so to add Reflector to your path:

Add-PathVariable Ppath "$( $env:programfiles )\Reflector"

Reply

Add comment



  Country flag
biuquote
  • Comment
  • Preview
Loading


About the author

Oisin Grehan lives in Montreal, Canada and builds all sorts of crap for all sorts of people.

Month List

Page List