3

I am using .NET4.0, but for compatability reasons, I'd like to compile to a .NET2.0 dll from c#. There should be no .NET4.0 specific functionality used in the script, so it should run fine in a .NET2.0 environment. Is there some commandline syntax on the csc that I can specify a version number with?

steventnorris
  • 5,656
  • 23
  • 93
  • 174
  • 3
    I believe you can specify that as your build target in the project properties. – kitti Mar 26 '12 at 15:32
  • http://msdn.microsoft.com/en-us/library/bb398202.aspx – Bryan Crosby Mar 26 '12 at 15:33
  • Are you saying you want to install a .Net 2.0 dll on a machine that ONLY has .Net 4.0 installed? Or would .Net 2/3/3.5 also be installed? – Brook Mar 26 '12 at 15:34
  • @Brook: .NET 4.0 is [backward compatible anyway](http://msdn.microsoft.com/en-us/library/ff602939.aspx) (as long as you don't do anything too exotic). – Robert Harvey Mar 26 '12 at 15:36
  • @Robert: Right but if 2.0 is also installed, an easy way to do it would be to just invoke the 2.0 version of csc. – Brook Mar 26 '12 at 15:44

5 Answers5

6

You mentioned csc.exe, so I'm assuming that you won't be building with Visual Studio, but rather, by command line. Also, I'm assuming that msbuild is not available on the build machine.

I believe that csc.exe is specific to each version. For example, in the folder C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319, you will find csc.exe, and in the folder C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727, you will find another version of csc.exe.

To build a .NET 2.0 dll, you should reference the csc.exe from the v2.0 folder (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727).

Cheers!

code4life
  • 15,655
  • 7
  • 50
  • 82
2

In Visual Studio you could set the target framework version to .NET 2.0 in the properties of the project:

enter image description here

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

if you are compiling manually from the command line, can't you just run the v2 framework csc?

eg (paths from my machine)

C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe

or for v4

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
0

If you build with MSBuild (which of course includes from within VS) then you can specify the target framework version from the project properties dialog. However, if you build manually it seems there is no surefire way to express that restriction.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
0

Set the target framework to 2.0 in the project's properties. In case you are using features like LINQ that are not present on the 2.0 framework, this approach won't work. If you need full compatibility with 2.0 framework, you should write your code for the 2.0 and then compile targeting the 4.0 later if you need.

Israel Lot
  • 653
  • 1
  • 9
  • 16