CSharpCodeProvider似乎陷入了.NET 2.0,我如何获得新function?

我有以下相当标准的代码作为CSharpCodeProvider的包装器。 这个类工作得很好,执行得很好,等等。但是,尽管我的应用程序是针对.NET 3.5构建的,并且在进行此编译时引用了v3.5程序集,但我仍然无法访问任何更好的C#3.5语法,如lambda或auto-properties。 有没有办法让这个工作?

我的印象是这个类只是围绕着csc.exe ,这个想法似乎被我的防火墙确认了(我的应用程序试图访问csc.exe )。 也许我只需要将options.CompilerOptions设置为某些东西?

 protected virtual void Compile() { Microsoft.CSharp.CSharpCodeProvider csProvider = new Microsoft.CSharp.CSharpCodeProvider(); CompilerParameters options = new CompilerParameters(); options.GenerateExecutable = false; options.GenerateInMemory = true; options.IncludeDebugInformation = true; foreach (string s in this.ReferencedAssemblies) { options.ReferencedAssemblies.Add(s); } CompilerResults result; string source = this.CodeTemplate; // [snip] Do some manipulation to fill in the template with values. result = csProvider.CompileAssemblyFromSource(options, source); this.HasErrors = result.Errors.HasErrors; this.Errors = new CompilerError[result.Errors.Count]; result.Errors.CopyTo(Errors, 0); if (HasErrors && ThrowOnErrors) throw new InvalidProgramException("The code currently stored in the " + this.GetType() + " cannot be compiled."); else if (HasErrors) return; this.CompiledAssembly = result.CompiledAssembly; } 

编辑:

我现在引用了mscorlibSystem.CoreSystem.Text和我自己的程序集之一。

你可以将一个编译器标志传递给构造函数(在字典中):

 Dictionary options = new Dictionary { { "CompilerVersion", "v3.5" } }; var compiler = new CSharpCodeProvider(options); 

无论如何,这对我有用…

在引用的程序集中,尝试添加对System.Core的引用。 应该这样做。 那里有很多3.5function。 如果不这样做,请打开当前项目的构建属性,并检查可能需要加载的其他程序集。