Demo: Exec-Inline, an Add-in that executes the currently selected code, immediately

Execute the code immediately under the cursor

You're in Visual Studio (2003 in this case)

  1. Highlight the code you want to run.
  2. Press the 'exec inline' button on the toolbar.
  3. The code is compiled and executed in its own little assembly

All credit goes to these four places:

Code compilation retyped from jconwell's Dot Net Script Project at CodeProject.

Wrapper around compilation retyped from dstang2000's DynamicCompileAndRun project.

Help with writing a VS Add-in: Scott Swigart, PasteAs Visual Basic Add-in

And help with getting the currently selected text from the IDE, retyped from Kevin McFarlane's VS Csharp Macros page.

I deliberately re-typed, rather than copying and pasting their code, so that I'd have to really look at what was going on in every place.

(continues with 'how does it work')

How does it work?

The code you select gets wrapped inside it's own class (unless it contains a class of its own) and then an entry point is added to this class. A bunch of common imports (i.e. include) statements are added, just in case.

It's compiled using System.CodeDom.Compiler which is remarkably easy to do.

If the code doesn't compile -- then I display a mini window, showing the generated code, and what compilation errors there were. This gives you a chance to alter the code and get it to work.

If there's a runtime exception, then I display the same window, along with the exception that occurred.

I don't have enough time to work on this project. So it's on the backburner, alongside ShinyPower Analyzer, WSCG v4, Crank, SimpleTests, and everything that isn't TimeSnapper or Advantech.

Writing this add-in was actual just a detour along the way toward writing a SimpleTests add-in, which I'm still kicking around in the back of my mind.

The steps and decisions involved in getting that puppy to fly are all too many.

Regarding exec immediate: Source code currently is VB only (since the PC i first developed it on didn't have C# installed). Once I've made it work for both languages, I'll post the project up.


I'm currently writing a book about how to build your first product. If you want to build your first product, please sign up to be notified when the book is available.

(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.