【发布时间】:2012-02-02 19:20:24
【问题描述】:
我正在尝试构建一个 Web 应用程序来在线评估 .Net 代码(如 http://ideone.com/) 我已经设法用 CodeDomProvider 编译代码。 但是我不知道如何将标准输入参数传递给这个函数。
如果用户在我的 Web 应用程序的 textarea 中编写此代码:
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
09OK
你知道怎么做吗?
【问题讨论】:
-
带有
Main()的网络应用程序?您说您想构建一个 Web 应用程序,但您正在处理更多用于控制台应用程序的标准输入。您确定不想要控制台应用程序吗? -
用户可以在文本区域中输入此代码的网络应用程序(如ideone.com)
-
标准输入参数是指命令行参数吗?
static void Main(string[] args) -
如果是 Web 应用程序,您不会从标准输入读取,也不会有 Main(),您只需从 TextBox 控件中获取文本。
-
@JamesMichaelHare - 我认为 Toc 的意思是他有一个网页,您可以在其中输入代码并动态编译它,他想知道如何将参数传递给编译后的产品。
标签: c# parameters stdin codedom