开发者

How can I get VS2010 to use the 3.0 C# compiler instead of 4.0?

How can I get VS2010 to use the 3.5 C# compiler instead of 4.0?

It it even possible, or do I need 开发者_如何转开发to have a separate VS2008 solution file to coerce compilation with the 3.0 compiler?

(updated to fix compiler version #)

Update: My motivation is to get the productivity of developing in VS2010, but it needs to run on a build server that may not have a single bit of .NET 4.0 binaries on it.


I don't believe you can do so. You can set a language version in the project properties, but that's not the same as using the C# 3 compiler from .NET 3.5. I believe there are subtle differences around type inference and overload resolution, for example, where the C# 4 rules will be followed instead of the C# 3 rules, even when building as C# 3. Here's an example of that:

using System;
class Test
{
    static void Main()
    {
        CallFunc(Demo);
    }

    static int Demo() { return 5; }

    static T CallFunc<T>(Func<T> func)
    {
        return func();
    }
}

This builds using the C# 4 compiler, even when you specify /langversion:3 - but it doesn't build with the C# 3 compiler.

You may find that you can tweak the project file to use the previous version of the compiler - but I don't know how Visual Studio would deal with that. I suspect it would get hideously confused.

EDIT: Okay, now you've given the motivation, I think you may want to do what we do for Noda Time. We have a VS2010 solution and a VS2008 solution file. They both load the same projects, which have a ToolsVersion attribute of 4.0. I haven't tried, but I believe that should build absolutely fine on a machine with only .NET 3.5.

You may well want to set the language version to 3 as well in your project files, to avoid accidentally using new features. It won't make your compatibility bullet-proof, but if you've got all your unit and integration tests building and running on a .NET 3.5-only continuous build server, you're unlikely to get bitten by the changes.


You can't. Visual Studio 2010 uses the 4.0 compiler even if you target framework 3.5.

Also, there is no version 3.5 of the C# compiler.


Can't be done. However, there's not much need for that as the C# 4 compiler is able to generate .NET 2.0-compatible assemblies (.NET 3.0 and 3.5 are just the 2.0 runtime engine + new compilers and libraries).

Are you sure you need the C# 3 compiler (ships with VS 2008 / .NET 3.5) or is having output compatible with it good enough?


if you have 3.5 installed you should be able to select the framework in the project properties. Rightclick on the projectname -> properties


You can have a project in VS2010 target the 3.5 framework (not the compiler).

Just double-click the properties folder in solution explorer (or right-click on the project name) and in the Properties page, on the Application tab, select the targeted framework. (You can select from Frameworks 4.0 down to 2.0.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜