简体   繁体   English

为什么 C# 引用在 NuGet 和 Visual Studio 之间添加不同

[英]Why are C# references added differently between NuGet and Visual Studio

We use NuGet (NuGet Version: 3.5.0.1996) two different ways.我们以两种不同的方式使用 NuGet(NuGet 版本:3.5.0.1996)。 Either we run it from the command line or we use the NuGet Package Manager in Visual Studio (2015).我们可以从命令行运行它,也可以使用 Visual Studio (2015) 中的 NuGet 包管理器。

The problem is that these two ways add references to the.csproj file with different formats.问题是这两种方式添加对.csproj 文件的引用格式不同。 If we use the command line, we get a reference that looks like this:如果我们使用命令行,我们会得到一个如下所示的引用:

<Reference Include="Dummy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
  <HintPath>..\packages\Dummy.1.27.10\lib\net452\Dummy.dll</HintPath>
  <Private>True</Private>
</Reference>

If we use the NuGet Package Manager in Visual Studio, we get a reference that looks like this:如果我们在 Visual Studio 中使用 NuGet 包管理器,我们会得到一个如下所示的引用:

<Reference Include="Dummy, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
  <HintPath>..\packages\Dummy.1.27.10\lib\net452\Dummy.dll</HintPath>
  <Private>True</Private>
</Reference>

Notice that ones adds the reference with the PublicKeyToken attribute and the other adds it with the processorArchitecture attribute.请注意,其中一个添加了带有 PublicKeyToken 属性的引用,另一个添加了带有 processorArchitecture 属性的引用。

This causes issues with our source control with frequent (and unnecessary) updates and merges.这会导致我们的源代码管理出现问题,频繁(和不必要的)更新和合并。

It would be nice to know why this happens, but I would much rather have a way to prevent it from happening.很高兴知道为什么会发生这种情况,但我更希望有一种方法来防止它发生。 Any suggestions?有什么建议么?

Starting with Visual Studio 2015, Nuget comes embedded and you cannot change the version used.从 Visual Studio 2015 开始,Nuget 是嵌入式的,您无法更改所使用的版本。 I don't have a VS 2015 at hand to check, but most likely it is using a version different than the one you have in the command line.我手边没有要检查的 VS 2015,但很可能它使用的版本与您在命令行中使用的版本不同。

Nuget has "suffered" (or enjoyed) a lot of changes and I think this is just a manifestation on how two different versions of nuget handles package references. Nuget“遭受”(或享受)了很多变化,我认为这只是两个不同版本的 nuget 如何处理包引用的体现。

My suggestion, to avoid having issues with source control, is to standardize one way of using Nuget and stick to it.为了避免源代码管理出现问题,我的建议是标准化一种使用 Nuget 的方法并坚持使用。 My suggestion would be to use it inside Visual Studio, either by means of Package Manager Console (similar to the CLI) or though the visual interface.我的建议是通过包管理器控制台(类似于 CLI)或通过可视化界面在 Visual Studio 中使用它。

I made a little research.我做了一点研究。 And decide to post an answer.并决定发布答案。

PublicKeyToken = null gives you information about that CLR is looking for the unsigned assembly. PublicKeyToken = null为您提供有关 CLR 正在寻找未签名程序集的信息。

  • What is the assembly in .net? .net 中的程序集是什么?

Assemblies form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions for a .NET-based application.程序集构成了基于 .NET 的应用程序的部署、版本控制、重用、激活范围和安全权限的基本单元。 Assemblies take the form of an executable (.exe) file or (in this case) dynamic link library (.dll) file , and are the building blocks of the .NET Framework.程序集采用可执行 (.exe) 文件或(在本例中)动态链接库 (.dll) 文件的形式,并且是 .NET Framework 的构建块。

  • What is public key token?什么是公钥令牌?

The public key token is a small number which is a convenient "token" representing a public key.公钥令牌是一个小数字,是一个方便的“令牌”,代表一个公钥。 Public keys are quite long;公钥很长; the purpose of the public key token is to let you refer to keys without saying the whole key.公钥令牌的目的是让您在不说出整个密钥的情况下引用密钥。 Sort of the same way saying "The Lord of the Rings" is five words which represent a half-a-million-word novel.有点像说“指环王”是五个词,代表一部 50 万字的小说。 It would be rather inconvenient if every time you wanted to talk about it, you had to state those half-a-million words.要是每次说起来都得说那五十万字,那就太不方便了。

When we know the definitions for assemblies and public key tokens, we can speak about them.当我们知道程序集和公钥令牌的定义时,我们就可以谈论它们。

When you add references to your project, they look different.当您添加对项目的引用时,它们看起来会有所不同。

  • Why?为什么? What could cause the difference?是什么导致了差异?

Add a file reference.添加文件引用。 The initial value of Specific Version in Properties panel is False. Properties面板中Specific Version的初始值为False。 The csproj file look like csproj 文件看起来像

<Reference Include="Name">
  <HintPath>...</HintPath>
</Reference>

Change Specific Version in Properties pane to True.将属性窗格中的特定版本更改为 True。 VS adds version in the Include attribute. VS 在 Include 属性中添加版本。

<Reference Include="Name, Version=...">
  <HintPath>...</HintPath>
</Reference>

Change Specific Version in Properties pane to False again.再次将属性窗格中的特定版本更改为 False。 VS adds a child element SpecificVersion. VS 添加了一个子元素 SpecificVersion。

<Reference Include="Name, Version=...">
  <HintPath>...</HintPath>
  <SpecificVersion>False</SpecificVersion>
</Reference>

So the final definition seems to be:所以最终的定义似乎是:

Which reference-type you get depends on how you link the assembly.您获得的引用类型取决于链接程序集的方式。

Your issues comes from incompatible assemblies.您的问题来自不兼容的程序集。 Not from the way how you references them.不是从你引用它们的方式。

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

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