[英]What's the best way to target multiple versions of the .NET framework?
I'm building a class library and I will deploy it a NuGet package, which lets me choose different assemblies to be added as references based on the .NET framework version of the project it's added to. 我正在构建一个类库,我将部署一个NuGet包,它允许我根据它添加到的项目的.NET框架版本选择要添加的不同程序集作为引用。 This is a very nice feature, but what I'm wondering is whether it is possible to have a single class library project, and build it against mulitple versions of the .NET framework? 这是一个非常好的功能,但我想知道的是,是否有可能拥有一个类库项目,并针对.NET框架的多个版本构建它?
I'd rather avoid having: 我宁愿避免:
MyLibrary40.dll
and MyLibrary45.dll
MyLibrary40.dll
和MyLibrary45.dll
if possible, because the two projects would have to share a lot of code. 如果可能的话,因为这两个项目必须分享很多代码。 The 4.5 version will be offering async
functions, which is a 4.5 feature. 4.5版本将提供async
功能,这是4.5功能。
Does anyone know what the best approach for this is? 有谁知道最好的办法是什么? Can I use multiple build configurations? 我可以使用多个构建配置吗? Or must I go down the separate project route? 或者我必须沿着单独的项目路线走?
If I was working in C++ I'd probably use multiple configurations and #if
blocks around the functions that are only supported in one configuration, but I worry this would lead to me having two assemblies with the same name that do different things. 如果我在C ++中工作,我可能会使用多个配置和围绕仅在一个配置中支持的函数的#if
块,但我担心这会导致我有两个具有相同名称的程序集执行不同的操作。
Thanks in advance! 提前致谢!
You will at least need one VisualStudio Solution with 2 projects (one for .net 4 and one for .net 4.5). 您将至少需要一个包含2个项目的VisualStudio解决方案(一个用于.net 4,另一个用于.net 4.5)。
Add all codefiles to the .net 4-project and in the other project you add the code files as link (use "Add Existing Item..."
-Dialog and chose Add as link
) 将所有代码文件添加到.net 4项目中,在另一个项目中添加代码文件作为链接(使用"Add Existing Item..."
对话”并选择“ Add as link
)
Now you add all codes and classes for .NET 4.5 to your 4.5-project. 现在,将.NET 4.5的所有代码和类添加到4.5项目中。
Additionally you should define your own compiler switches (conditional compilation symbols) to your projects. 另外,您应该为项目定义自己的编译器开关(条件编译符号)。 Like NET4 for your .net 4-project and NET4.5 to your .net 4.5-project) 像.NET4一样用于.net 4项目,NET4.5用于.net 4.5项目
You set the switches in the project settings under Build->General->Conditional Compilation Switches 您可以在Build-> General-> Conditional Compilation Switches下的项目设置中设置开关
In your code you can use the switches as follows to generate code for .NET 4 or .NET 4.5 在您的代码中,您可以使用以下开关生成.NET 4或.NET 4.5的代码
#if NET4
// code only for .NET 4
#endif
// code for all framework versions.
#if NET45
// code only for .NET 4.5
#endif
A simple approach is to add another .csproj
file in the same folder, and configure it to build a different framework version. 一种简单的方法是在同一文件夹中添加另一个.csproj
文件,并将其配置为构建不同的框架版本。 This avoids having to add links to files, as both projects are essentially views over the same folder structure. 这避免了必须添加文件的链接,因为两个项目本质上是对同一文件夹结构的视图 。
Say you have the structure: 说你有结构:
- MyLibrary\
- MyLibrary.sln
- MyLibrary\
- MyLibrary.csproj
- Program.cs
Duplicate MyLibrary.csproj
to the same folder and edit to change a few things: 将MyLibrary.csproj
复制到同一文件夹并编辑以更改以下内容:
<ProjectGuid>
just make a new GUID for this element's value <ProjectGuid>
只为该元素的值创建一个新的GUID <TargetFrameworkVersion>
specify the alternative version here, eg: v4.5
or v3.5
<TargetFrameworkVersion>
在此指定替代版本,例如: v4.5
或v3.5
<OutputPath>
(for Debug and Release) set this to a unique path, such as bin\\Debug\\net45
and bin\\Debug\\net45
, to allow each project's output to end up in a unique location <OutputPath>
(用于Debug和Release)将其设置为唯一路径,例如bin\\Debug\\net45
和bin\\Debug\\net45
,以允许每个项目的输出结束于一个唯一的位置 You must also add a new element to the non-conditional <PropertyGroup>
element, so that the two projects don't collide in the obj
folder during parallel builds. 您还必须向非条件<PropertyGroup>
元素添加新元素,以便在并行构建期间两个项目不会在obj
文件夹中发生冲突。 This is important, and protects against weird race condition bugs. 这很重要,可以防止奇怪的竞争条件错误。
<PropertyGroup>
<BaseIntermediateOutputPath>obj\net45\</BaseIntermediateOutputPath>
Finally, add this new project to your existing solution. 最后,将此新项目添加到现有解决方案中。
This approach works hand in hand with defining compilation switches such as NET35
and NET45
, and using #if NET35
/ #endif
directives. 这种方法与定义编译开关(如NET35
和NET45
)以及使用#if NET35
/ #endif
指令#if NET35
。
Two open source projects that use this technique are MetadataExtractor and NetMQ . 使用此技术的两个开源项目是MetadataExtractor和NetMQ 。 You can refer to them in case you hit trouble. 如果您遇到麻烦,可以参考它们。
我知道的老问题,当时这不是一个合适的答案......但是现在可以使用共享项目,这在逻辑上与将项目添加为链接而不是单独添加每个文件相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.