试图将stdin传递给主方法以在线评估代码
本文关键字:方法 在线评估 代码 stdin | 更新日期: 2023-09-27 18:19:23
我正在尝试构建一个web应用程序来在线评估。net代码(如http://ideone.com/)我已经设法用CodeDomProvider编译代码。但是我不知道如何传递stdin参数给这个函数。
如果用户在我的web应用程序的textarea中编写了以下代码:
using System;
public class Test {
static void Main()
{
String t = "";
while (t != null){
t = Console.ReadLine();
Console.WriteLine(t+"OK");
}
}
}
我可以用下面的代码编译它编译信息[]allCompilerInfo = CodeDomProvider.GetAllCompilerInfo();
//Tell the compiler what language was used
CodeDomProvider CodeProvider = CodeDomProvider.CreateProvider("C#");
//Set up our compiler options...
CompilerParameters CompilerOptions = new CompilerParameters();
var _with1 = CompilerOptions;
_with1.ReferencedAssemblies.Add("System.dll");
_with1.GenerateInMemory = true;
_with1.TreatWarningsAsErrors = true;
//Compile the code that is to be evaluated
CompilerResults Results = CodeProvider.CompileAssemblyFromSource(CompilerOptions, strCode);
现在我想管理泛型参数:
如果我像这样传递一个示例参数:
53岁
12
09年
我想要编译的结果:
53好
12好
09年好
你知道怎么做吗?
当我解释这个问题时,您正在询问如何从用户输入文本框的代码中运行并将stdin命令行数据传递给在web服务器上编译的应用程序。为了避免这样做的严重的安全问题,你需要做的是使用System.Diagnostics.Process类启动应用程序,并在StartInfo属性中指定RedirectStandardInput。首先阅读Process类的MSDN文档:http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx