string Code = context.Request["Code"];
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();

// 2.ICodeComplier
ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();

// 3.CompilerParameters
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
string DllPath = ConfigurationManager.AppSettings["DllPath"];
objCompilerParameters.ReferencedAssemblies.Add(DllPath+"KO.dll");
objCompilerParameters.ReferencedAssemblies.Add(DllPath + "UBIEngine.API.Common.dll");
objCompilerParameters.ReferencedAssemblies.Add(DllPath + "UBIEngine.CommunicationArchitecture.API.Client.dll");
//objCompilerParameters.ReferencedAssemblies.Add("http://" + "localhost:5006" + "/DLL/UBIEngine.CommunicationArchitecture.API.Client.dll");
objCompilerParameters.GenerateExecutable = false;
objCompilerParameters.GenerateInMemory = true;

// 4.CompilerResults
CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, Code);

if (cr.Errors.HasErrors)
{
Console.WriteLine("编译错误:");
var result = "";
foreach (CompilerError err in cr.Errors)
{
result = err.ErrorText;
Console.WriteLine(err.ErrorText);
}
string json = "{success:'false',result:'" + result + "'}";
context.Response.Write(json);
}
else
{
string json = "{success:'true'}";
context.Response.Write(json);
}

posted on 2018-06-13 10:16  刘祥伟  阅读(110)  评论(0编辑  收藏  举报