首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# codeDom误差

C# codeDom误差
EN

Stack Overflow用户
提问于 2014-01-12 18:03:05
回答 1查看 7.7K关注 0票数 1

我试图用这个函数用codeDom编译代码:

代码语言:javascript
复制
   public static bool Compile(string Output, string Source, string Icon, string resources)
   {
      CompilerParameters Parameters = new CompilerParameters();
      CompilerResults cresults = default(CompilerResults);
      Dictionary<string, string> providerOptions = new Dictionary<string, string>();
      providerOptions.Add("CompilerVersion", "v2.0");
      CSharpCodeProvider Compiler = new CSharpCodeProvider(providerOptions);
      Parameters.GenerateExecutable = true;
      Parameters.TreatWarningsAsErrors = false;
      Parameters.OutputAssembly = Output;
      Parameters.EmbeddedResources.Add("System");

      Parameters.CompilerOptions = "/target:winexe /platform:x86";
      if (!string.IsNullOrEmpty(Icon))
      {
         Parameters.CompilerOptions += " /win32icon" + Icon;
      }
      cresults = Compiler.CompileAssemblyFromSource(Parameters, Source);

      if (cresults.Errors.Count > 0)
      {
         foreach (CompilerError compile_error in cresults.Errors)
         {
            CompilerError error = compile_error;
            MessageBox.Show(error + "");
         }
         return false;
      }
      return true;
   }

源有这些libarys (不确定它是不是libarys,我是一个java开发人员):

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using System.Data.SQLite;
using System.Data;
using System.Text;
using System.Collections.Specialized;
using System.Net;
using Dns = System.Net.Dns;
using AddressFamily = System.Net.Sockets.AddressFamily;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.ComponentModel;
using System.IO;
using System.Security.Cryptography;

但是,当我编译时,我会得到以下错误:

代码语言:javascript
复制
c:\Users\Augustin\AppData\Local\Temp\odq0sdk5.0.cs(3,14) : error CS0234: The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\Users\Augustin\AppData\Local\Temp\odq0sdk5.0.cs(4,14) : error CS0234: The type or namespace name 'Xml' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\Users\Augustin\AppData\Local\Temp\odq0sdk5.0.cs(5,14) : error CS0234: The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\Users\Augustin\AppData\Local\Temp\odq0sdk5.0.cs(6,14) : error CS0234: The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\Users\Augustin\AppData\Local\Temp\odq0sdk5.0.cs(8,26) : error CS0234: The type or namespace name 'Specialized' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?)
c:\Users\Augustin\AppData\Local\Temp\odq0sdk5.0.cs(9,14) : error CS0234: The type or namespace name 'Net' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\Users\Augustin\AppData\Local\Temp\odq0sdk5.0.cs(16,14) : error CS0234: The type or namespace name 'ComponentModel' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\Users\Augustin\AppData\Local\Temp\odq0sdk5.0.cs(10,20) : error CS0234: The type or namespace name 'Net' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\Users\Augustin\AppData\Local\Temp\odq0sdk5.0.cs(11,30) : error CS0234: The type or namespace name 'Net' does not exist in the namespace 'System' (are you missing an assembly reference?)
c:\Users\Augustin\AppData\Local\Temp\odq0sdk5.0.cs(60,14) : error CS0246: The type or namespace name 'DataTable' could not be found (are you missing a using directive or an assembly reference?)

我如何解决这些错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-12 18:08:10

没有添加引用的程序集的问题。所有这些错误都与应该添加的另一个程序集相关:

代码语言:javascript
复制
Parameters.ReferencedAssemblies.Add("System.dll"); // System, System.Net, etc namespaces
Parameters.ReferencedAssemblies.Add("System.Data.dll"); // System.Data namespace
Parameters.ReferencedAssemblies.Add("System.Data.SQLite.dll"); // System.Data.SqlLite namespace
Parameters.ReferencedAssemblies.Add("System.Xml.dll"); // System.Xml namespace
Parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll"); // System.Windows.Forms namespace

如果您的代码使用这些命名空间,那么所有适当的程序集都应该添加到ReferencedAssemblies集合中。

PS。我不知道SQLite程序集的正确程序集名称是什么,因为我猜它是System.Data.SQLite.dll,但我可能错了

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21078298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档