简体   繁体   English

从.NET4.0编译.NET2.0 DLL

[英]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#. 我正在使用.NET4.0,但是出于兼容性原因,我想从c#编译为.NET2.0 dll。 There should be no .NET4.0 specific functionality used in the script, so it should run fine in a .NET2.0 environment. 该脚本中不应使用特定于.NET4.0的功能,因此它应在.NET2.0环境中正常运行。 Is there some commandline syntax on the csc that I can specify a version number with? csc上是否可以使用一些命令行语法来指定版本号?

You mentioned csc.exe , so I'm assuming that you won't be building with Visual Studio, but rather, by command line. 您提到了csc.exe ,所以我假设您不会使用Visual Studio构建,而是通过命令行构建。 Also, I'm assuming that msbuild is not available on the build machine. 此外,我假设msbuild在构建计算机上不可用。

I believe that csc.exe is specific to each version. 我相信csc.exe特定于每个版本。 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 . 例如,在文件夹C:\\WINDOWS\\Microsoft.NET\\Framework\\v4.0.30319 ,您将找到csc.exe ,在文件夹C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727 ,您将找到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 ). 要构建.NET 2.0 dll,应从v2.0文件夹( C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727 )引用csc.exe

Cheers! 干杯!

In Visual Studio you could set the target framework version to .NET 2.0 in the properties of the project: 在Visual Studio中,您可以在项目的属性中将目标框架版本设置为.NET 2.0:

在此处输入图片说明

if you are compiling manually from the command line, can't you just run the v2 framework csc? 如果您是从命令行手动编译的,那么您是否只能运行v2框架csc?

eg (paths from my machine) 例如(来自我机器的路径)

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

or for v4 或对于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. 如果您使用MSBuild进行构建(当然它是从VS内提供的),则可以从项目属性对话框中指定目标框架版本。 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. 在项目的属性中将目标框架设置为2.0。 In case you are using features like LINQ that are not present on the 2.0 framework, this approach won't work. 如果您使用的是LINQ之类的功能,而这些功能在2.0框架中不存在,则此方法将无效。 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. 如果需要与2.0框架完全兼容,则应为2.0编写代码,然后在以后根据需要针对4.0进行编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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