c# - 如何在运行时在浏览器中运行代码
问题描述
我一直在使用这里的文档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这样的体验
解决方案
Suchiman / Robin Sue 在这个漂亮的 blazor 项目中集成了 Monaco 编辑器和浏览器内 C# 编译器(现场演示)
推荐阅读
- typescript - 类型检查对象而不使用类型保护?
- python - Python给出子进程的进程ID
- java - 如何让Android返回一个内部类对象来flutter
- javascript - 登录时将用户重定向到主页
- c# - Identityserver 4 基于角色的登录
- apache-spark - spark shuffle 耗时太长
- amazon-ec2 - Prestashop 错误使用 AWS CloudFront 后重定向过多
- mysql - 列组合的外键
- tensorflow - 使用pygame和tensorflow在python中实现神经网络
- sql - case when in.. where 在 select 语句中