Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: how do i create a program that will run code that i punch into a text box?

Tags:

c#

ide

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?

like image 750
Sinaesthetic Avatar asked Dec 01 '10 21:12

Sinaesthetic


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


3 Answers

Here is a good link on the CompilerServices namespace:

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

like image 146
Achilles Avatar answered Sep 20 '22 13:09

Achilles


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!");
like image 29
jackjumper Avatar answered Sep 18 '22 13:09

jackjumper


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

like image 28
Sorax Avatar answered Sep 19 '22 13:09

Sorax