CSharpCodeProvider似乎在.NET 2.0被卡住,我如何获得新的特点? [英] CSharpCodeProvider seems to be stuck at .NET 2.0, how do I get new features?

查看:913
本文介绍了CSharpCodeProvider似乎在.NET 2.0被卡住,我如何获得新的特点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的,比较规范的代码周围 CSharpCodeProvider 的包装。这个类的工作非常好,并执行就好了,等等,等等。但是,尽管我的应用程序建立在.NET 3.5和这样的编译时引用v3.5版本组件,我还没有得到访问任何像拉姆达的或自动性的额外漂亮的C#3.5的语法。是否有某种方式来得到这个工作?

I have the following, fairly standard code as a wrapper around CSharpCodeProvider. This class works very well, and performs just fine, etc, etc. But, despite the fact that my application is built against .NET 3.5 and is referencing v3.5 assemblies when doing this compilation, I still don't get access to any of the extra nice C# 3.5 syntax like lambda's or auto-properties. Is there some way to get this working?

我的印象是,这个类只是缠 CSC.EXE ,一个想法,似乎是下通过我的防火墙证实了(我的应用程序试图访问 CSC.EXE )。也许我只需要设置 options.CompilerOptions 来的东西吗?

I was under the impression that this class just wrapped around csc.exe, an idea that would seem to be confirmed by my firewall (my application tries to access csc.exe). Perhaps I just need to set options.CompilerOptions to something?

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;
}



编辑:

我要 mscorlib程序 System.Core程序引用System.Text 和此刻的我自己的组件之一。

I have references to mscorlib, System.Core, System.Text and one of my own assemblies at the moment.

推荐答案

有一个编译器标志你可以通过到构造函数(在词典中):

There's a compiler flag that you can pass into the constructor (in a dictionary):

Dictionary<string,string> options = new Dictionary<string,string>
{ 
    { "CompilerVersion", "v3.5" }
};

var compiler = new CSharpCodeProvider(options);



这就是对我工作的,反正...

That's what's worked for me, anyway...

这篇关于CSharpCodeProvider似乎在.NET 2.0被卡住,我如何获得新的特点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆