【问题标题】:The type or namespace name 'Process' does not exist in the namespace 'System.Diagnostics'命名空间“System.Diagnostics”中不存在类型或命名空间名称“Process”
【发布时间】:2017-08-03 17:16:41
【问题描述】:

这个错误对我来说完全没有意义。 我正在使用 CodeDOM 编译可执行文件。 这是我的编译类:

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,但我产生了类似且不太具体的错误),并且出于某种原因我收到这个错误。我将不胜感激。

【问题讨论】:

    标签: c# .net codedom system.diagnostics


    【解决方案1】:

    您没有将引用传递给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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 2022-01-11
      • 2021-10-15
      相关资源
      最近更新 更多