编码编译类型或名称空间错误



i具有使用.NET 4 Framework(不是客户端配置文件)的WPF的程序,并且它编译了一个称为" source.txt"的源文件。但是,每当它编译时,我都会收到此错误"错误CS02345:命名空间'系统'中不存在类型或名称空间'Windows'(您是否缺少汇编引用?)"。没有创建文件。

当我检查源.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");

如果此信息还不够,请询问。

作为错误明显提示,您需要将System.Windows.Forms.dll添加到ReferencedAssemblies

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

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

最新更新