我试图用这个函数用codeDom编译代码:
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开发人员):
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;但是,当我编译时,我会得到以下错误:
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?)我如何解决这些错误?
发布于 2014-01-12 18:08:10
没有添加引用的程序集的问题。所有这些错误都与应该添加的另一个程序集相关:
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,但我可能错了
https://stackoverflow.com/questions/21078298
复制相似问题