c#:如何创建一个程序来运行我在文本框中输入的代码?

c#:如何创建一个程序来运行我在文本框中输入的代码?,c#,ide,C#,Ide,我想知道我如何才能创建一个小应用程序来接受我的输入并作为代码运行。与构建IDE一样,但我不想走那么远,但我确实希望它使用FCL。比如说,如果我有一个文本框,我输入: string x = "hello"; int myLength; myLength = x.length; 然后点击submit按钮,它将编译此文件并将其运行到输出窗口中。有没有一个简单的方法可以做到这一点?还是我必须纠正一个相当复杂的方法来打破这一切?对不起,如果我不是很清楚的话,我对如何问这个问题不是很肯定,哈哈。我真正想

我想知道我如何才能创建一个小应用程序来接受我的输入并作为代码运行。与构建IDE一样,但我不想走那么远,但我确实希望它使用FCL。比如说,如果我有一个文本框,我输入:

string x = "hello";
int myLength;

myLength = x.length;

然后点击submit按钮,它将编译此文件并将其运行到输出窗口中。有没有一个简单的方法可以做到这一点?还是我必须纠正一个相当复杂的方法来打破这一切?对不起,如果我不是很清楚的话,我对如何问这个问题不是很肯定,哈哈。我真正想做的是,我想创建一种代码“计算器”工具,在这里我可以输入一些简单的代码片段并测试它返回的内容,而不需要构建一个完整的类并编译来测试一些简单的东西。有人能给我指出正确的方向吗?

你可以先看一下前面的SO帖子。

这里有一个关于CompilerServices命名空间的好链接:

在C语言中,这并不容易,因为C语言是一种编译语言,而不是解释语言

在解释语言中,将原始文本解析为语言并查看结果相当容易


也许你会更乐意输入JavaScript或Perl并解释它?

除了已经给出的答案之外,还允许你动态运行C代码(如果你愿意,也可以从文件中运行)

您可以使用CSScript

从他们的网站:

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

我想你要找的是。它可以让你运行少量代码,而无需制作一次性的控制台应用程序来测试东西。只是为了好玩,我写了以下内容,将编译和执行文本框中的任何内容

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);
}
void Main()
{
TextBox TextBox=新的TextBox()
{
多行=真,
尺寸=新尺寸(200,60),
Text=@“string x=”“hello”“;int myLength;myLength=x.Length;MessageBox.Show(myLength.ToString());”
};
按钮按钮=新按钮()
{
位置=新点(0,60)
};
单击+=新建事件处理程序(委托(对象发送者,事件参数e)
{
var csc=new CSharpCodeProvider(new Dictionary(){{{“compilervision”,“v3.5”});
var参数=新的编译器参数(新[]{“mscorlib.dll”、“System.dll”、“System.Windows.Forms.dll”},“foo.exe”,true);
parameters.GenerateExecutable=true;
CompilerResults results=csc.compileasemblyFromSource(参数,
@"
使用System.Windows.Forms;
命名空间控制台应用程序
{
班级计划
{
静态void Main(字符串[]参数)
{
" + 
文本框,文本
+ @"
}
}
}
");                
processproc=Process.Start(“foo.exe”);
}
);
表格f=新表格
{
控件={textBox,button}
};
应用程序。运行(f);
}

实际上,我是在
LINQPad
中编写的,但是在
visualstudio

中运行非常容易,在这种情况下,CodeDom不是一种好方法。看看你是否只是想要这个工具,下载