2017-08-03 267 views
0

此錯誤對我來說絕對沒有意義。 我使用CodeDOM編譯可執行文件。 這裏是我的類編譯:名稱空間'System.Diagnostics'中不存在類型或名稱空間名稱'Process'

using System; 
using System.CodeDom.Compiler; 
using System.IO; 
using Microsoft.CSharp; 

class Compiler 
{ 
    public static bool Compile(string[] sources, string output, params 
string[] references) 
    { 
     var results = CompileCsharpSource(sources, "result.exe"); 
     if (results.Errors.Count == 0) 
      return true; 
     else 
    { 
     foreach (CompilerError error in results.Errors) 
      Console.WriteLine(error.Line + ": " + error.ErrorText); 
    } 
    return false; 
} 

    private static CompilerResults CompileCsharpSource(string[] sources, 
string output, params string[] references) 
    { 
     var parameters = new CompilerParameters(references, output); 
     parameters.GenerateExecutable = true; 
     using (var provider = new CSharpCodeProvider()) 
      return provider.CompileAssemblyFromSource(parameters, sources); 
    } 
} 

這裏是我如何編譯我的源:

Compiler.Compile(srcList, "test.exe", new string[] { "System.dll", "System.Core.dll", "mscorlib.dll" }); 

這裏的地方發生錯誤的源代碼,我編寫的部分:

System.Diagnostics.Process p; 
if (System.Diagnostics.Process.GetProcessesByName("whatever").Length > 0) 
    p = System.Diagnostics.Process.GetProcessesByName("whatever")[0]; 
else 
    return false; 

所以我在編譯時引用了System.dll,而且我在進程前寫了System.Diagnostics(我也嘗試過使用System.Diagnostics,但是我生成了一個類似的, ic錯誤),出於某種原因,我得到這個錯誤。我會很感激一些幫助。

回答

2

您未通過對CompileCsharpSource的引用。

變化Compile這樣:

public static bool Compile(string[] sources, string output, params string[] references) 
{ 
    var results = CompileCsharpSource(sources, "result.exe", references); 
    if (results.Errors.Count == 0) 
      return true; 
    else 
    { 
     foreach (CompilerError error in results.Errors) 
      Console.WriteLine(error.Line + ": " + error.ErrorText); 
    } 
    return false; 
} 
相關問題