Demo: Exec-Inline, an Add-in that executes the currently selected code, immediately
You're in Visual Studio (2003 in this case)
- Highlight the code you want to run.
- Press the 'exec inline' button on the toolbar.
- The code is compiled and executed in its own little assembly
All credit goes to these four places:
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.
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.
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.Next → ← Previous
My book "Choose Your First Product" is available now.
It gives you 4 easy steps to find and validate a humble product idea.