FastSharp - Write it, Execute it

Published 11 February 08 11:30 PM | MattManela 

Last year I wrote this program which I named FastSharp.  It is a text editor which lets you compile and run C# code that would normally exist inside a method.  The inspiration for this came from getting tired of opening up Visual Studio and creating a project when all I want to do is execute one line of code and see what it outputs. 

A prime example of this happened today (which is what sparked me to finally post this program).  My co-worker Bertan and I were talking about the System.DateTime object in .NET.  We were wondering if the ToBinary() method would return the same value regardless  whether the datetime object was in local time or in UTC mode.  He was about to fire up Visual Studio and create a project just to write this two line program when I showed him FastSharp.

First I opened the program:

image

 

Then I entered this two line program and hit the run button:

image

 

I got an error because I forgot the capitalize the second t in ToUniversalTime().  So, I corrected that and then ran it again:

image

 

And then we had our answer.  The to outputs are not the same so they do produce different results. 

The way FastSharp works is pretty simple.  It takes whatever you enter and wraps it in a Main method which is then wrapped in a class which then has a list of import statements appended above it (configurable through the settings dialog).  It then compiles the code using the Microsoft.CSharp.CSharpCodeProvider class and executes it.  Because FastSharp wraps the code inside of a method block you can only write code that would normally compile inside of a method. (No classes or methods).

 

Things To Consider and Future Consideration

  1. FastSharp can only do console output not input.  I was unable to figure out how to make Console.ReadLine() work inside the program.  This will be an important future feature if someone can figure it out.
  2. FastSharp compiles only C# code but it is not hard to make it work for Visual Basic, which would also be a nice feature upgrade.
  3. FastSharp uses the out-of-the-box winforms RichTextBox control as the code editor.  It would be nice to replace this with a code editor that color coded or even give intellisense :)

 

Give Me! Give Me! Give Me!

To download both the FastSharp binary and source code visit FastSharp Code Gallery Page.

Comments

# MSDN Blog Postings » 2008 » February » 12 said on February 12, 2008 3:58 AM:

PingBack from http://msdnrss.thecoderblogs.com/2008/02/12/

# { The Smoking Code } said on February 12, 2008 8:05 AM:

FastSharp - Write it, Execute it

# Mighell's Mobile Blog said on February 12, 2008 8:17 AM:

Ricordo che un po' di tempo fa c'era in giro un tool simile. In sostanza, si tratta di un piccolo

# Bite my bytes said on February 24, 2008 5:25 PM:

Links of the Week #25 (week 8/2008)

# Zunanji viri said on February 24, 2008 6:12 PM:

Development Top-10 Application-Design Mistakes - How not to design your UI. It's hard not to nod

Anonymous comments are disabled

About MattManela

I am a software developer at Microsoft. My blog is http://blogs.msdn.com/matt
Page view tracker