我一直在使用这里的文档https://support.microsoft.com/en-gb/help/304655/how-to-programmatically-compile-code-using-c-compiler我正在尝试更多地了解编译器我想在我自己的网站上托管一个简单的文本编辑器,我可以用它来运行脚本代码,说一些简单的事情
程序需要打印出 Console.WriteLine("Hello World");
如果打印出除 Hello World 以外的任何内容,则程序将出错。
我一直在查看有关在运行时运行 .net 代码的 Microsoft 代码,但这两者都迫使它创建一个 exe 我希望结果就像文本框中的 .net fiddle。
我想我必须做些什么来运行 exe 并使用该进程返回结果,记住这是在 mvc 应用程序中。
或者他们有什么很酷的 nugets 可以节省我在这里的时间。
private void Compiler(string code)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
string Output = "Out.exe";
System.CodeDom.Compiler.CompilerParameters parameters = new
CompilerParameters();
//Make sure we generate an EXE, not a DLL
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromSource(parameters, code);
if (results.Errors.Count > 0)
{
foreach (CompilerError CompErr in results.Errors)
{
CompilerError error = new CompilerError();
error.Line = CompErr.Line;
error.ErrorNumber = CompErr.ErrorNumber;
error.ErrorText = CompErr.ErrorText;
}
}
else
{
//Successful Compile
CodeResult result = new CodeResult();
result.Message = "Success";
}
}
那么如何捕获上述内容并返回,以及如何添加对其他语言(如 python 或 vb.net)的支持
这是 blazor 可能擅长为我做的事情吗?
我想提供像 .net fiddle https://dotnetfiddle.net这样的体验
Suchiman / Robin Sue 在这个漂亮的 blazor 项目(现场演示)中集成了摩纳哥编辑器以及浏览器内的 C# 编译器
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句