[英]Compile C# Code In The Application
我想要一些代码来编译我的TextBox中的代码(例如)。 我的意思是我想在运行程序后编译代码。 我怎样才能做到这一点?
看到这篇文章:
http://support.microsoft.com/kb/304655
这是他们提供的示例代码:
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在他的回答中提到的那样,要小心这个解决方案。 您需要确保已编译的代码最终在其自己的AppDomain
,否则您将遇到内存泄漏。
有关如何将代码加载到单独的AppDomain
请参阅此相关问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.