2017-08-03 266 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; 
} 
相关问题