6

i was wondering how exactly I can create a small app that will accept my input and run it as code. same light as building an IDE, but I dont want to go that far, but I do want it to use the FCL. So like if I had a text box and i punched in:

string x = "hello";
int myLength;

myLength = x.length;

Then hit a submit button, it would compile this and run it into an output window. Is there a pretty simple way to do this? Or am i going to have to right a pretty involved method that breaks this all up? Sorry if i'm not being totally clear, I wasn't exactly positive on how to ask this question lol. Really what I'm getting at, is I want to create sort of a code "calculator" tool where I can punch in some simple snippets and test what it is returning without having to build a full class and compiling just to test something simple. can anyone point me in the right direction?

Amicable
  • 3,115
  • 3
  • 49
  • 77
Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176

6 Answers6

3

Here is a good link on the CompilerServices namespace:

http://www.codeproject.com/KB/vb/DotNetCompilerArticle.aspx

Achilles
  • 11,165
  • 9
  • 62
  • 113
3

You could use CSScript

http://www.csscript.net/

From their web site:

dynamic script = CSScript.LoadCode(@"using System;
                                 public class Script
                                 {
                                     public void SayHello(string greeting)
                                     {
                                         Console.WriteLine(greeting);
                                     }
                                 }")
                                .CreateObject("*");
script.SayHello("Hello World!");
jackjumper
  • 163
  • 4
3

I think what you're looking for is LINQPad. It'll let you run small bits of code without the overhead of making little one-off console apps to test things. Just for fun I wrote the following that'll compile and execute whatever is in the textbox.

void Main()
{
    TextBox textBox = new TextBox()
    {
        Multiline = true,
        Size = new Size(200, 60),
        Text = @"string x = ""hello"";int myLength;myLength = x.Length;MessageBox.Show(myLength.ToString());"
    };
    Button button = new Button()
    {
        Location = new Point(0, 60)
    };
    button.Click += new EventHandler(delegate(object sender, EventArgs e)
        {
            var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
            var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.dll", "System.Windows.Forms.dll" }, "foo.exe", true);
            parameters.GenerateExecutable = true;
            CompilerResults results = csc.CompileAssemblyFromSource(parameters,
            @"
            using System.Windows.Forms;

            namespace ConsoleApplication
            {
                class Program
                {
                    static void Main(string[] args)
                    {
                        " + 
                        textBox.Text 
                        + @"
                    }
                }
            }
            ");                
            Process proc = Process.Start("foo.exe");
        }
    );

    Form f = new Form
    {
        Controls = { textBox, button }
    };
    Application.Run(f);
}

I actually wrote it in LINQPad but it would be very easy to get running in Visual Studio

Sorax
  • 2,205
  • 13
  • 14
0

You can start by taking a look at this previous SO post.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
  • 1
    CodeDom is not good approach in this case. see http://stackoverflow.com/questions/2366921/reflection-emit-vs-codedom – Saeed Amiri Dec 01 '10 at 21:17
0

In C# this won't be so easy, because C# is a compiled language, not an interpreted language.

In an interpreted language, it's fairly easy to parse raw-text as the language, and see the results.

Maybe you'd be happier typing in JavaScript or Perl and interpreting that?

Michael Paulukonis
  • 9,020
  • 5
  • 48
  • 68
0

In addition to the answers already given, PowerShell allows you to run C# code on the fly (or from a file, if you wish).

Mark Avenius
  • 13,679
  • 6
  • 42
  • 50