简体   繁体   中英

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?

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!

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

在此处输入图片说明

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM