【发布时间】:2020-01-10 05:27:57
【问题描述】:
我一直在使用这里的文档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 应用程序中。
或者他们有什么很酷的方法可以节省我在这里的时间。
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这样的体验
【问题讨论】:
-
“在浏览器中”,您提到的 Blazor 可能是您问题中唯一在浏览器中运行的东西。 ASP.NET 在服务器上运行,而不是在浏览器中。你能澄清你的问题吗?
-
@LasseVågsætherKarlsen 是的,我改变了我的问题,我想做类似 .net fillde 在浏览器中运行代码并输出 dotnetfiddle.net
-
dotnetfiddle 不在浏览器中运行它,而是在服务器上运行它,他们只是对它进行大量沙箱处理。
-
我所说的“大量沙箱化”是指:如果您不确定可以使用哪种技术通过 Web 浏览器正确运行用户提供的 C#/.NET 代码,不要!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!如果你做错了,你将把你的整个服务器都打开到互联网上。
-
@LasseVågsætherKarlsen 说这是一个真实世界的应用程序,我正在试验这项技术以更好地学习和理解 .net fiddle 通过学习以完全相同的方式开始
标签: c# asp.net-mvc asp.net-core blazor