编译C#code中的应用程序 [英] Compile C# Code In The Application

查看:194
本文介绍了编译C#code中的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要一些code,它编译code,它是在我的文本框(例如)。我的意思是我要运行该程序后编译code。我怎样才能做到这一点?

I want some code that compiles the code that is in my TextBox (for example). What I mean is I want to compile code after running the program. How can I do this?

推荐答案

请参阅这篇文章:

http://support.microsoft.com/kb/304655

下面是他们提供的样品code:

Here's the sample code they provide:

var codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();

var parameters = new CompilerParameters()
{
    GenerateExecutable = true,
    OutputAssembly = Output,
};
CompilerResults results = icc.CompileAssemblyFromSource(parameters, sourceString);

if (results.Errors.Count > 0)
{
    foreach(CompilerError error in results.Errors)
    {
        textBox2.Text = textBox2.Text
            + "Line number " + error.Line
            + ", Error Number: " + error.ErrorNumber
            + ", '" + error.ErrorText + ";"
            + Environment.NewLine + Environment.NewLine
            ;
    }
}

由于Aliostad在他的回答提到,要小心使用此解决方案,虽然。你需要确保你的编译code在自己的的AppDomain 结束,否则你会遇到内存泄漏。

As Aliostad has mentioned in his answer, be careful with this solution, though. You will need to make sure your compiled code ends up in its own AppDomain, otherwise you will experience memory leaks.

请参阅如何将code加载到一个单独的的AppDomain 此相关的问题:

See this related question on how to load the code into a separate AppDomain:

<一个href=\"http://stackoverflow.com/questions/1799373/how-can-i-$p$pvent-compileassemblyfromsource-from-leaking-memory\">How我可以prevent CompileAssemblyFromSource泄漏内存?

这篇关于编译C#code中的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆