我正在尝试使用此函数使用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;
}
source有这些libarys(不确定它是libarys我是一个java develloper):
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?)
如何解决这些错误?
答案 0 :(得分:2)
您未添加引用的程序集的问题。所有这些错误都与应该添加的另一个程序集相关:
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,但我可能是错的