C# 了解 csproj 程序集参考

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16578819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:23:24  来源:igfitidea点击:

Understanding a csproj assembly reference

c#visual-studio-2010reference.net-assemblycsproj

提问by intangible02

I am using VS2010 and I tried to add a few assemblies from local hard disk to my C# project through file reference. Peeking into the csprojfile, I found sometimes the file reference appears as

我正在使用 VS2010,我尝试通过文件引用将一些程序集从本地硬盘添加到我的 C# 项目中。查看csproj文件,我发现有时文件引用显示为

<Reference Include="name">

However sometimes it appears as

但是有时它显示为

<Reference Include="name, Version=xxx, Culture=neutral, 
                                          processorArchitecture=MSIL">

What could cause the difference?

什么可能导致差异?



Inspired by k3b's answer, I did another test. I created a new class library project.

受k3b回答的启发,我又做了一个测试。我创建了一个新的类库项目。

  1. Add a file reference. The initial value of Specific Versionin Propertiespane is False. The csprojfile look like

    <Reference Include="Name">
      <HintPath>...</HintPath>
    </Reference>
    
  2. Change Specific Versionin Propertiespane to True. VS adds version in the Includeattribute.

    <Reference Include="Name, Version=...">
      <HintPath>...</HintPath>
    </Reference>
    
  3. Change Specific Versionin Propertiespane to Falseagain. VS adds a child element SpecificVersion.

    <Reference Include="Name, Version=...">
      <HintPath>...</HintPath>
      <SpecificVersion>False</SpecificVersion>
    </Reference>
    
  1. 添加文件引用。的初始值特定版本属性面板是。该csproj文件看起来像

    <Reference Include="Name">
      <HintPath>...</HintPath>
    </Reference>
    
  2. 更改特定版本属性窗格。VS 在Include属性中添加版本。

    <Reference Include="Name, Version=...">
      <HintPath>...</HintPath>
    </Reference>
    
  3. 更改特定版本属性窗格中一次。VS 添加了一个子元素SpecificVersion

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

So it seems that the rule is:

所以似乎规则是:

  • When Versionis present in Includeattribute and there is no SpecificVersionchild element, the file assembly is configured to be Specific Version
  • The SpecificVersionchild element is only appended with value False.
  • Include属性中存在Version并且没有子元素时,文件程序集配置为特定版本SpecificVersion
  • SpecificVersion子元素只与价值附加

One thing I still do not understand:

我仍然不明白的一件事:

  • For my new test project, if I remove the file reference and add it back again, it goes back to format in point 1, which is the default.
  • For my existing project, if I remove the file reference and add it back again, I get back format in point 3. Although it also means that Specific Version is not used, I am wondering why it does not go back to format in point 1.
  • 对于我的新测试项目,如果我删除文件引用并重新添加它,它会返回到第 1 点中的格式,这是默认设置。
  • 对于我现有的项目,如果我删除文件引用并重新添加它,我会在第 3 点中恢复格式。虽然这也意味着未使用特定版本,但我想知道为什么它不会回到第 1 点中的格式.

采纳答案by k3b

Which reference-type you get depends on how you link the assembly.

您获得哪种引用类型取决于您如何链接程序集。

  • select the referenced assembly in the project-explorer
  • go to the properties-page
  • 在项目资源管理器中选择引用的程序集
  • 转到属性页

there you find a boolean flag "specific Version"

在那里你会发现一个布尔标志“特定版本”

  • true means: the assembly must have version=xxx
  • false means: ignore the assembly version
  • true 表示:程序集必须具有 version=xxx
  • false 表示:忽略程序集版本

(I only have a german-vs2010 so the english translation for the german "Spezifische Version" may be slightly different)

(我只有一个 German-vs2010,所以德语“Spezifische 版本”的英文翻译可能略有不同)

[update]

[更新]

I tried the following using vcs2010-express german

我使用 vcs2010-express German 尝试了以下操作

add reference with default SpecificVersion=False: no version

添加使用默认 SpecificVersion=False 的引用:无版本

<Reference Include="Castle.Core">
  <HintPath>..\..\..\lib\fluentNHibernate\Castle.Core.dll</HintPath>
</Reference>

modified reference: SpecificVersion=True: added version

修改后的参考:SpecificVersion=True:添加的版本

<Reference Include="Castle.Core, Version=2.5.1.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
  <HintPath>..\..\..\lib\fluentNHibernate\Castle.Core.dll</HintPath>
</Reference>

modified reference again: SpecificVersion=False: version remains and new element SpecificVersion

再次修改参考:SpecificVersion=False:版本保持不变,新元素SpecificVersion

<Reference Include="Castle.Core, Version=2.5.1.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
  <HintPath>..\..\..\lib\fluentNHibernate\Castle.Core.dll</HintPath>
  <SpecificVersion>False</SpecificVersion>
</Reference>

It seems that the specific version is remembered but ignorede because of <SpecificVersion>False</SpecificVersion>

似乎记住了特定版本但被忽略了,因为 <SpecificVersion>False</SpecificVersion>