【问题标题】:CodeDom Compile The Type or Namespace ErrorCodeDom 编译类型或命名空间错误
【发布时间】:2012-10-21 20:51:35
【问题描述】:

我有一个在 C# 中使用 WPF 的程序,它使用 .NET 4 Framework(不是客户端配置文件),它编译一个名为“Source.txt”的源文件。但是,无论何时编译它,我都会收到此错误“错误 CS02345:名称空间‘系统’中不存在类型或名称空间名称‘Windows’(您是否缺少程序集引用?)”。没有创建文件。

当我检查 Source.txt 文件中的行时,这些是给出错误的行:

using System.Windows.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Forms;
using System.Management;

这是我用来从主程序编译它的代码:

CompilerParameters Params = new CompilerParameters();
            Params.GenerateExecutable = true;
            Params.ReferencedAssemblies.Add("System.dll");
            Params.OutputAssembly = ServerNameBox.Text;
            Params.CompilerOptions = " /target:winexe";    

            string Source = compileSource;
            CompilerResults Results = new CSharpCodeProvider().CompileAssemblyFromSource(Params, Source);

            if (Results.Errors.Count > 0)
            {
                foreach (CompilerError err in Results.Errors)
                    System.Windows.Forms.MessageBox.Show(err.ToString());
            }
            else System.Windows.Forms.MessageBox.Show("Sucessfully Compiled Program!");

如代码所示,我希望将此程序编译为 Windows 窗体/GUI 应用程序(“/target:winexe”);

如果这些信息不充分,请询问。

【问题讨论】:

    标签: c# namespaces codedom


    【解决方案1】:

    正如错误明确提示,您需要将System.Windows.Forms.dll 添加到ReferencedAssemblies

    【讨论】:

    • 哦,我明白了。谢谢,这清除了那部分,但现在我收到一个错误,说我需要一个静态入口点。当我将我的主要方法设为静态时,它迫使我将其余的变量和方法也设为静态。有没有其他方法可以做到这一点?
    • @Hexo:与编写任何其他应用程序的方式相同
    • 对不起,你能澄清一下你的陈述吗?
    【解决方案2】:

    您至少需要添加以下引用:

    Params.ReferencedAssemblies.Add("PresentationFramework.dll");
    Params.ReferencedAssemblies.Add("System.Windows.Forms.dll");
    Params.ReferencedAssemblies.Add("System.Management.dll");
    Params.ReferencedAssemblies.Add("PresentationCore.dll");
    

    我也不相信 System.Windows.Linq 是一个实际的命名空间。如果您需要 LINQ,它应该是 System.Linq,并且在 System.Core.dll 中

    【讨论】:

    • PresentationFramework.dll & PresentationCore.dll 无法找到。我相信编译的应用程序我不使用 System.Windows.Input 所以我只是删除了它。不过还是谢谢。
    猜你喜欢
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多