简体   繁体   English

如何仅针对特定目标框架从 NuGet package 还原程序集

[英]How to restore assemblies from a NuGet package for a specific target framework only

I'm looking for a way to restore the assemblies for a NuGet package which targets exactly one framework, in this case net45 .我正在寻找一种方法来恢复 NuGet package 的程序集,它只针对一个框架,在本例中为 net45

This is my packages config:这是我的包配置:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Newtonsoft.Json" version="12.0.1" targetFramework="net45" />
</packages>

This is my NuGet config file:这是我的 NuGet 配置文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <config>
        <add key="repositoryPath" value=".\Nuget" />
    </config>
</configuration>

Version is: NuGet Version: 5.2.0.6090版本为: NuGet Version: 5.2.0.6090

Running: nuget restore packages.config -ConfigFile nuget.config restores the assemblies for all targetframework versions as can be seen by:运行: nuget restore packages.config -ConfigFile nuget.config恢复所有targetframework版本的程序集,如下所示:

E:\Tmp\NuGet\Nuget\Newtonsoft.Json.12.0.1>dir lib
 Directory of E:\Tmp\NuGet\Nuget\Newtonsoft.Json.12.0.1\lib

2019-09-30  18:27    <DIR>          .
2019-09-30  18:27    <DIR>          ..
2019-09-30  18:27    <DIR>          net20
2019-09-30  18:27    <DIR>          net35
2019-09-30  18:27    <DIR>          net40
2019-09-30  18:27    <DIR>          net45
2019-09-30  18:27    <DIR>          netstandard1.0
2019-09-30  18:27    <DIR>          netstandard1.3
2019-09-30  18:27    <DIR>          netstandard2.0
2019-09-30  18:27    <DIR>          portable-net40+sl5+win8+wp8+wpa81
2019-09-30  18:27    <DIR>          portable-net45+win8+wp8+wpa81

According to docs.microsoft.com根据docs.microsoft.com

When NuGet installs a package that has multiple assembly versions, it tries to match the framework name of the assembly with the target framework of the project.当 NuGet 安装具有多个程序集版本的 package 时,它会尝试将程序集的框架名称与项目的目标框架匹配。

If a match is not found, NuGet copies the assembly for the highest version that is less than or equal to the project's target framework, if available.如果未找到匹配项,NuGet 会复制小于或等于项目目标框架(如果可用)的最高版本的程序集。 If no compatible assembly is found, NuGet returns an appropriate error message.如果未找到兼容的程序集,NuGet 将返回相应的错误消息。

For example, consider the following folder structure in a package:例如,考虑 package 中的以下文件夹结构:

 \net45 \MyAssembly.dll \net461 \MyAssembly.dll

When installing this package in a project that targets .NET Framework 4.6, NuGet installs the assembly in the net45 folder, because that's the highest available version that's less than or equal to 4.6.在以 .NET 框架 4.6、NuGet 为目标的项目中安装此 package 时,会将程序集安装在 net45 文件夹中,因为这是低于或等于 4 的最高可用版本。

If the project targets .NET Framework 4.6.1, on the other hand, NuGet installs the assembly in the net461 folder.另一方面,如果项目针对 .NET 框架 4.6.1,则 NuGet 将程序集安装在 net461 文件夹中。

From the paragraph above I understand that when I set the target framework I should be able to restore just the assemblies for that one target framework.从上面的段落我了解到,当我设置目标框架时,我应该能够只恢复那个目标框架的程序集。 In my case it looks like NuGet completely ignores the targetFramework attribute and always installs the dlls for all target frameworks.在我的情况下,它看起来像 NuGet 完全忽略了targetFramework属性并始终为所有目标框架安装 dll。 Changing it from net45 to net40 has no effect.将其从net45更改为net40无效。

How can I make NuGet restore ONLY the dlls in a package for a specific target framework?如何让 NuGet 仅恢复package中特定目标框架的 dll?

Try to change your targetFramework from尝试将您的 targetFramework 从

targetFramework="net45"

to

targetFramework="4.5"

I don't think you can.我不认为你可以。

提琴手

The nuget (zip) contains all the frameworks. nuget (zip) 包含所有框架。

All Nuget.exe is doing is downloading the file and extracting it. Nuget.exe所做的只是下载文件并解压缩。 You could delete the ones you don't want afterwards?以后不想要的可以删吗?

Also, why not just use .\nuget.exe restore -Force -NoCache -Packagesdirectory.\Nuget instead of the nuget config file?另外,为什么不直接使用.\nuget.exe restore -Force -NoCache -Packagesdirectory.\Nuget而不是 nuget 配置文件?

You could confuse people by creating your own package that just contains your framework!您可以通过创建自己的仅包含您的框架的 package 来迷惑人们!

For unity, you can configure which .dll version to use for which platforms with the *.dll.meta files.为了统一,您可以使用*.dll.meta文件配置哪个.dll版本用于哪个平台。

Get the packages获取包裹

Specify where to put the packages either on the command line or in a nuget.config file so that they will be included as assets in unity.指定在命令行或nuget.config文件中放置包的位置,以便将它们作为统一的资产包含在内。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
      <add key="globalPackagesFolder" value="./NuGetPackages" />
  </config>
</configuration>

If you use packages.config set the repositoryPath instead of the globalPackagesFolder .如果您使用packages.config设置repositoryPath而不是globalPackagesFolder

This can go in the same folder as your dotnet solution, in which case you can restore the packages with dotnet restore or nuget restore in the solution folder.这可以 go 与您的 dotnet 解决方案位于同一文件夹中,在这种情况下,您可以使用解决方案文件夹中的dotnet restorenuget restore来恢复包。

Configure which version is used in Unity配置在 Unity 中使用哪个版本

Find the dll files from the package从 package 中找到 dll 文件

来自 nuget 包的 Unity dll 文件

Open the file in the inspector and disable it for platforms, or conditionally for build constraints.在检查器中打开文件并为平台禁用它,或有条件地为构建约束禁用它。

禁用所有平台的 Unity dll 检查器

This gets saved into the .dll.meta files in the same directory as the .dll .这将保存到与 .dll 位于同一目录中的.dll.meta .dll中。 You don't want them to be deleted so...您不希望它们被删除,所以...

Configure source control to keep the .dll.meta files配置源代码管理以保留.dll.meta文件

This .gitignore will exclude everything from the packages, but will keep the .dll.meta files that specify which version Unity uses..gitignore将从包中排除所有内容,但将保留指定 Unity 使用的版本的.dll.meta文件。

**/NuGetPackages/**
!**/NuGetPackages/**/
!**/NuGetPackages/**/*.dll.meta

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

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