Skip to main content
 首页 » 编程设计

c#之尝试将标准输入传递给主要方法以在线评估代码

2024年02月23日20arxive

我正在尝试构建一个 Web 应用程序来在线评估 .Net 代码(例如 http://ideone.com/) 我已经设法用 CodeDomProvider 编译代码。 但是我不知道如何将标准输入参数传递给这个函数。

如果用户在我的网络应用程序的文本区域中编写此代码:

using System; 
public class Test  { 
 
    static void Main() 
    { 
        String t = ""; 
        while (t != null){ 
           t = Console.ReadLine(); 
            Console.WriteLine(t+"OK"); 
        } 
     } 
} 

我可以用这段代码编译它 CompilerInfo[] 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

我想要编译的结果:
53OK
12OK
09确定

你知道怎么做吗?

请您参考如下方法:

在我解释这个问题时,您问的是如何运行 stdin 命令行数据并将其传递给应用程序,该应用程序是根据用户在文本框中键入的代码在您的 Web 服务器上编译的。为避免这样做带来的严重安全问题,您要做的是使用 System.Diagnostics.Process 类启动应用程序,并在 StartInfo 属性中指定 RedirectStandardInput。首先阅读有关 Process 类的 MSDN 文档:http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx