从具有可移植类库的NuGet包中定位.NET 4的最佳策略 [英] Best strategy to target .NET 4 from a NuGet package with a portable class library

查看:56
本文介绍了从具有可移植类库的NuGet包中定位.NET 4的最佳策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为当前仅针对.NET 4实现的库提供了NuGet软件包.但是我已经移植了库代码以支持各种平台(WinRT,SL5,WP8),因此理想情况下,我希望将其打包为可移植类.库(PCL)可以简化维护.但是该库使用的是LINQ to XML(XELements等),它要求定位.NET 4.0.3并在客户端计算机上安装.NET 4.0.3.

I have a NuGet package for a library that is currently implemented only for .NET 4. But I have ported library code to support various platforms (WinRT, SL5, WP8) so ideally I would like to package it as a portable class library (PCL) to simplify the maintenance. But the library is using LINQ to XML (XELements etc.) that requires targeting .NET 4.0.3 and installting .NET 4.0.3 on a client machine.

因此,我对如何定位普通的.NET 4感到困惑.如果不是与NuGet包有关,并且可以控制用户群,我可以简单地将其作为安装.NET 4.0.3运行时的前提条件.但是,我不想以任何方式限制用户群,因此看起来我将不得不拥有该库的两个版本:面向.NET 4.5,SL5和WP8的可移植版本以及仅面向.NET4的非便携式版本.愚蠢的是,这两个库将具有完全相同的代码,因为.NET 4当然支持LINQ to XML,而仅当PCL面向.NET 4时才没有这种支持.

So I have a dilemma regarding how to target plain .NET 4. If it was not about NuGet packages and I had a control of the user base I could simply state as a prerequisite installing .NET 4.0.3 runtime. However, I don't want to limit the user base in any way, so it looks like I will have to have two versions of the library: portable that targets .NET 4.5, SL5 and WP8 and non-portable targeting just .NET4. What's silly is that both libraries will have exactly the same code since LINQ to XML is of course supported in .NET 4, it's just PCLs that don't have such support when targeting .NET 4.

我的第一个问题是这是否似乎是正确的策略?替代方法是从PCL中删除所有XElement依赖的代码,并将其放在不可移植的部分,但这似乎不正确,因为所有库的代码都完全相同.

My first question is whether this seems to be a right strategy? The alternative would be to take away all XElement-dependent code from PCL and have it in non-portable parts, but this does not seem right because the code will be exactly the same for all libraries.

第二个问题是,从PCL中完全定位.NET 4.0.3是否有意义:如果我具有针对.NET 4的单独版本,则安装了.NET 4.0.3运行时的用户将从获得PCL中获得任何收益而不是普通的.NET 4版本?我知道.NET 4.0.3还有其他改进,但这些改进不会影响我的库.

The second question is whether it makes sense to target .NET 4.0.3 from a PCL at all: if I have separate version targeting .NET 4 will users that have .NET 4.0.3 runtime installed gain anything from getting a PCL rather than plain .NET 4 version? I know .NET 4.0.3 has other improvements but those don't affect my library.

推荐答案

是的,我认为最好的办法是创建库的两个版本,一个针对.NET 4,另一个针对可支持的其他平台的可移植库.使用源文件链接,因此您不必具有两个不同的源代码副本,而不必具有两个不同的Visual Studio项目.

Yes, I think the best thing is to create two versions of your library, one targeting .NET 4 and another portable library that targets the other platforms you support. Use source file linking so you don't have to have two different copies of your source code, just two different Visual Studio projects.

尽管您只需要一个NuGet软件包.将.NET 4版本放入lib/net40,将可移植版本放入lib/portable-net403 + win8 + wp8 + sl5(或您决定支持的平台的任何组合).然后,NuGet将根据项目的目标安装正确的对象.要使此版本适用于便携式版本,必须使用NuGet 2.1或更高版本.

You only need one NuGet package though. Put the .NET 4 version in lib/net40 and the portable version in lib/portable-net403+win8+wp8+sl5 (or whatever combination of platforms you decide to support). Then NuGet will install the right one depending on what a project is targeting. NuGet 2.1 or higher is required for this to work for the portable version.

关于.NET 4.0.3的问题,是关于为库的使用者提供灵活性.使用您的库的人正在使用它来创建应用程序.支持.NET 4对他们而言可能并不那么重要.通过在库的可移植版本中支持.NET 4.0.3,这意味着,如果他们选择在其应用程序中需要.NET 4.0.3,则他们将能够从自己的可移植库中使用您的库,并且更容易

In reference to the question about .NET 4.0.3, it's about giving the consumers of your library flexibility. The people using your library are using it to create applications. Supporting .NET 4 may not be as important to them as it is to you. By supporting .NET 4.0.3 in the portable version of your library, it means that if they choose to require .NET 4.0.3 in their apps, then they will be able to use your library from their own portable libraries and more easily share their app code across platforms.

这篇关于从具有可移植类库的NuGet包中定位.NET 4的最佳策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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