steventnorris
steventnorris

Reputation: 5896

Compile a .NET2.0 DLL from .NET4.0

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?

Upvotes: 3

Views: 1889

Answers (5)

Bob Vale
Bob Vale

Reputation: 18474

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

Upvotes: 2

code4life
code4life

Reputation: 15794

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!

Upvotes: 6

Israel Lot
Israel Lot

Reputation: 653

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.

Upvotes: 0

Jon
Jon

Reputation: 437734

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.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039398

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

enter image description here

Upvotes: 2

Related Questions