2017-08-03 27 views
0

このエラーはわかりません。 私はCodeDOMを使用して実行可能ファイルをコンパイルしています。ここ はコンパイルするための私のクラスです:'Process'という名前の型または名前空間が 'System.Diagnostics'という名前空間に存在しません。

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; 
} 
関連する問題