以.NET Framework的多个版本为目标的最佳方法是什么? [英] What's the best way to target multiple versions of the .NET framework?

查看:60
本文介绍了以.NET Framework的多个版本为目标的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个类库,我将为其部署一个NuGet程序包,该程序包使我可以基于要添加的项目的.NET Framework版本选择要添加的不同程序集作为引用.这是一个非常好的功能,但是我想知道的是,是否有可能拥有一个类库项目,并针对多个版本的.NET框架进行构建?

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. 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?

我宁愿避免:

MyLibrary40.dll MyLibrary45.dll

如果可能的话,因为这两个项目必须共享很多代码.4.5版将提供 async 功能,这是4.5版的功能.

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.

有人知道这样做的最佳方法是什么?我可以使用多个构建配置吗?还是我必须沿着单独的项目路线走?

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?

如果我使用C ++,可能会在仅支持一种配置的函数周围使用多种配置和 #if 块,但是我担心这会导致我有两个程序集与具有不同功能的相同名称.

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.

提前谢谢!

推荐答案

您至少需要一个带有2个项目的VisualStudio解决方案(一个用于.net 4,一个用于.net 4.5).

You will at least need one VisualStudio Solution with 2 projects (one for .net 4 and one for .net 4.5).

将所有代码文件添加到.net 4-项目,在另一个项目中,将代码文件添加为链接(使用添加现有项..." -对话框,然后选择添加作为链接)

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.5的所有代码和类添加到4.5项目中.

Now you add all codes and classes for .NET 4.5 to your 4.5-project.

此外,您应该为项目定义自己的编译器开关(条件编译符号).就像您的.net 4-项目的NET4和.NET 4.5项目的NET4.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)

您可以在Build-> General-> Conditional Compilation Switches下的项目设置中设置这些开关

You set the switches in the project settings under Build->General->Conditional Compilation Switches

在代码中,您可以使用以下开关为.NET 4或.NET 4.5生成代码

In your code you can use the switches as follows to generate code for .NET 4 or .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

这篇关于以.NET Framework的多个版本为目标的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆