Reflector.Snippy Addin

Currently I'm reading Jon Skeet's book: C# in Depth: What you need to master C# 2 and 3 - which I highly recommend.  For a learning tool, Jon created a utility named Snippy (you can download it from his site's Downloads page).  Snippy is a light weight snippet compiler you can use to follow along with the examples in his book (BTW: all the sippets are also downloadable from his site).  After using Jon's Snippy a little while, I found myself opening the assemblies in Reflector to see what was really being created by the C# compiler ... which started me thinking about moving the Snippy code to a Reflector addin to save myself one step (plus I was working on the Reflector addin presentation for the Seattle Code Camp).

Addin Download Link: Reflector.Snippy.zip or Reflector.Snippy.dll (latest version is 0.9.2.0 - you can verify your version in the About dialog)

If you are reading Jon's book and want to be able to run the Chapter 8 and Chapter 11 snippets, you'll want to read the section below on "Settings".

Main Functionality

Reflector.Snippy adds a submenu named 'Snippy' to the Tools menu and a couple of buttons to the ToolBar (a button for toggling the display of Snippy and another button to cause a build).

image

Display Snippy Toggles the display of the Snippy Window
Build Attempts to build the code in the Snippy Window.  Any build messages (including error messages from the build) are written to the Output Window - which only shows after a Build has been called for.
Remove Annotations Not really used - it was something the publisher used.  If you really want to know - look at the code.
New Clears the Snippy (source) window to create a new Snippet
Open Opens an existing snippet file.  See the setting section below on how to default the Snippet directory.
Save Saves the code in the Snippy window as a snippet file.  This is designed to save a temporary snippet - if you want to reopen later, you should use the Save As snippet.  See the setting section below on how this file is named.
Save As Allows you to assign a snippet name and pick the location to save the snippet.
Export Exports or allows the user to save the code in the Snippy window (wrapped with the templated code) as a C# file.
About Snippy Displays the credits for the addin.

The screen below is an example of opening and building an existing snippet from Jon's book (the snippet shown is from Chapter 11).  As you can see, Reflector shows you all the details of the tricks the compiler performs ... which is exactly the kind of stuff Jon talks about in his book.

image

Settings

In order to match all the functionality Jon's Snippy has, I created a couple of options dialogs.  You can get to the settings page by going to the View menu and choosing the Options item.  Once the options dialog is visible, you'll need to choose Snippy in the list to see the settings page.

image

Snippet Path Allows the user to set the default directory the addin looks for and/or saves snippets.

NOTE: if you have downloaded Jon's snippets, you'll want to set this directory to where you saved those snippets.
Output Path Allows the user to set the default directory the dll/exe will be saved in
Assembly name The assembly name to give the compiled snippet
Target Framework The .Net framework to target (only 2.0 and 3.5)
Output type Allows the user to either create a dll or an exe
Select snippet Main after compile Indicates whether the Main method should be automatically selected after a successful build occurs.
Imports button Launches the References and Namespaces dialog

image The purpose of the References and Namespace dialog is to provide the same options that Jon offers with his Snippy (shown here in the combo box):

image

The options are provided for determining three things at compile time: the version of the framework to compile for, the assembly references to set and the namespace imports to add (for the code to compile without typing all the fully qualified names).

If you are using Reflector.Snippy and not really running Jon's snippets, then you can type the references and the namespace imports you want to use.  All entries should only take a single line (the code uses an Environment.NewLine string for a delimiter).

NOTE: to add references that are not the System.* dll's, you'll need to include the complete path to the dll in the text box.

Example: If you want to use the "Chapter8" option for the compiler settings, then you have 2 options to reference the Chapter8.exe:

1.  Save the Chapter8.exe into the directory you have set as the Output Path on the Options dialog.

2.  Change the "Chapter8.exe" line in the text box to the complete path of the Chapter8.exe

Feedback

If you have any feedback or problems with the addin, please email or leave a comment on this blog entry.  If anyone is interested in adding color coding or intellisense type functionality to a RichTextbox - please contact me :)

posted on Sunday, November 23, 2008 1:37 PM

Feedback

# re: Reflector.Snippy Addin

Nice job Jason,
One nice feature would be to have an option to import currently selected assembly and it's references as well as adding a using statement matching the selected namespace? That would help out a lot when testing small snippets.
/WW
11/23/2008 8:21 PM | Wictor

# re: Reflector.Snippy Addin

Great Add-In. Thanks very much.

I intended to do something similar at some point that would highlight the difference between Release and Debug compiles. Would it be simple to add this to your Snippy add-in? I'm guessing you'd compile two assemblies, and I'm not sure how you'd display that in Reflector.NET.

All the same, thanks for this Add-In. I'll use it quite a lot I expect.
11/24/2008 4:12 AM | Drew Noakes

# re: Reflector.Snippy Addin

Wictor: Interesting idea, I'll look into that - it shouldn't be too hard to do.

Drew: Good idea, adding the ability to compile a debug and release wouldn't be too involved, but you'll still be left with the assembly comparison problem ... which is a little hard to provide a really user friendly comparison.
11/24/2008 5:37 AM | Jason Haley

# re: Reflector.Snippy Addin

Drew: Version 0.9.2.0 has a checkbox on the option page now to indicate a debug build or not, this may help you get started :)
11/26/2008 1:05 PM | Jason Haley

# Add-In Reflector : Reflector.Snippy, le Snippy de Jon Skeet sous forme d’un Add-In

Décidément, c’est riche en add-ins en ce moment. Cette fois ci il s’agit de l’intégration à Reflector
11/30/2008 3:41 AM | CoqBlog

Post Comment

Title  
Name  
Url
Comment   
Please enter the following code into the box below to stop spammers

  
Enter Code Here *