我使用MSDN上的教程和其他一些资源构建了一个c#编译器,包括这里,我已经让它工作,直到我添加其他参考程序集。我的错误源于将“System.dll”和“System.Windows.Forms.dll”添加到ReferenceAssemblies列表。
这是我的代码:
private void SetUpCompilingParameters()
{
string ver = string.Format("{0}.{1}.{2}", Environment.Version.Major, Environment.Version.MajorRevision, Environment.Version.Build);
string libDir = string.Format(@"{0}", Environment.CurrentDirectory);
string raDir = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0";
string exWpfDir = string.Format(@"C:\WINDOWS\Microsoft.NET\Framework\v{0}\WPF", ver);
string exDir = string.Format(@"C:\WINDOWS\Microsoft.NET\Framework\v{0}", ver);
MyCompiler = new CSharpCodeProvider();
CompilingParam = new CompilerParameters();
CompilingParam.GenerateExecutable = false;
CompilingParam.GenerateInMemory = true;
CompilingParam.IncludeDebugInformation = false;
CompilingParam.TreatWarningsAsErrors = false;
CompilingParam.CompilerOptions = string.Format("/lib:{0}", libDir);
CompilingParam.CompilerOptions = string.Format("/lib:{0}", raDir);
CompilingParam.CompilerOptions = string.Format("/lib:{0}", exDir);
//CompilingParam.CompilerOptions = string.Format("/lib:{0}", exWpfDir);
CompilingParam.ReferencedAssemblies.Add("System.dll");
CompilingParam.ReferencedAssemblies.Add("System.Windows.Forms.dll");
}
正如您所看到的,我在CompilerOptions中明确引用了目录,但它没有帮助。我想在here on stackoverflow上测试解决方案 利用:
CompilingParam.ReferencedAssemblies.Add(typeof(System.Xml.Linq.Extensions).Assembly.Location);
但我在使用它时遇到一般的System.dll等问题......