我有以下,相当标准的代码作为包装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;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我引用mscorlib,System.Core,System.Text和此刻的我自己的组件之一.
Jon*_*eet 25
有一个编译器标志,你可以传递给构造函数(在字典中):
Dictionary<string,string> options = new Dictionary<string,string>
{
{ "CompilerVersion", "v3.5" }
};
var compiler = new CSharpCodeProvider(options);
Run Code Online (Sandbox Code Playgroud)
无论如何,这对我有用...