繁体   English   中英

如何自动将解决方案中的所有项目设置为相同版本?

[英]How do I automatically set all projects in my solution to the same version?

我有一个包含 5 个 C# 项目的 Visual Studio 解决方案。 我希望程序集版本在每个项目中都匹配。 但看起来我必须将 go 放入每个项目的属性中,然后一次单击每个项目的程序集版本。 有没有办法将解决方案视为只有一个适用于每个项目的整体版本?

只需在解决方案根文件夹中创建一个文件,例如GlobalAssemblyInfo.cs ,然后向其中添加必要的属性,最后将其作为现有项目作为链接添加到每个项目中。

Solution Explorer右键单击project name > Add > Existing item...然后在对话框中从下拉列表中选择Add As Link选项,如您在此图像中所见。

// Content of GlobalAssemblyInfo.cs file
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

请注意:

  • 您必须从每个项目的Properties\\AssemblyInfo.cs文件中删除这些属性。
  • 您也可以将其他程序集属性移动到GlobalAssemblyInfo.cs文件中

结果是您将只有一个可以设置版本的文件,它将适用于所有项目。

更新#1:

.NET 5项目中,默认情况下,在构建期间会自动生成AssemblyInfo.cs文件。

好像只有7个属性是自动生成的:

  • AssemblyCompanyAttribute
  • AssemblyProductAttribute
  • AssemblyConfigurationAttribute
  • AssemblyVersionAttribute
  • AssemblyFileVersionAttribute
  • AssemblyInformationalVersionAttribute
  • AssemblyTitleAttribute

您在这里有两个选择:

  • 禁用AssemblyInfo.cs文件的自动生成。
  • 启用AssemblyInfo.cs文件的自动生成并关闭特定属性的生成。

创建一个文件(名称: Directory.Build.props )并将其放在.sln文件旁边,以便将其应用于解决方案中的所有项目。

示例 #1 - 禁用AssemblyInfo.cs文件的自动构建

Directory.Build.props :

<Project>
  <PropertyGroup>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
</Project>

示例 #2 - 仅禁用特定属性生成

在这种情况下,只需添加<Generate...>false</Generate...>行以禁用特定属性,其中...是属性类型名称。

Directory.Build.props :

<Project>
  <PropertyGroup>
    <GenerateAssemblyInfo>true</GenerateAssemblyInfo>
    <GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
    <GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
    <GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
  </PropertyGroup>
</Project>

备注

了解有关 SDK 样式项目文件中的AssemblyInfo 属性的更多信息。

此更新也适用于 .NET Core 版本。

如果特定项目有特殊需求,您也可以覆盖.csproj文件中的这些设置。

至于我,我通常把属性如下:

  • GlobalAssemblyInfo.cs
    • AssemblyCompanyAttribute
    • AssemblyProductAttribute
    • AssemblyCopyrightAttribute
    • AssemblyConfigurationAttribute
    • AssemblyTrademarkAttribute
    • AssemblyCultureAttribute
    • AssemblyVersionAttribute
    • AssemblyFileVersionAttribute
    • AssemblyInformationalVersionAttribute
    • ComVisibleAttribute
  • AssemblyInfo.cs (在特定项目中)
    • AssemblyTitleAttribute
    • AssemblyDescriptionAttribute
    • GuidAttribute

我认为没有任何解决方案级别的选项可以做到这一点。 我使用 powershell 脚本在一个解决方案中为我的 15 个项目实现它。

  $version= "1.3.0.0" 
  (Get-ChildItem -Include AssemblyInfo.cs -Recurse ) | 
     Foreach-Object { 
         Set-Content $_ ((Get-content $_ -Encoding UTF8) -replace "\d+\.\d+\.(\d+|\*)(\.(\d+|\*))?", $version)  -Encoding UTF8 
    }

将此脚本保存在与解决方案文件相同的目录中。 您还可以将其添加为解决方案本身中的解决方案项,并在您右键单击脚本文件时从 Visual Studio 命令行选项启动它。

由于每个项目都有自己的程序集,因此它们将被独立处理。 我发现最适合我的是使用 T4 模板并基于模板中的算法,例如这样的修订是自 2000 年 1 月 1 日以来的几个小时的计算:

<#@ template debug="false" hostspecific="false" language="C#" #>
using System.Reflection;
[assembly: AssemblyVersion("1.0.6.<#= this.RevisionNumber #>")]
[assembly: AssemblyFileVersion("1.0.6.<#= this.RevisionNumber #>")]
<#+
    int RevisionNumber = (int)(DateTime.UtcNow - new DateTime(2000,1,1)).TotalHours;
#>

由于 MSBuild 15 (Visual Studio 2017) ( https://docs.microsoft.com/en-us/visualstudio/msbuild/what-s-new-in-msbuild-15-0 ) 你可以使用Directory.Build.targets文件为了这。

  1. 在 your.sln 文件所在的位置创建Directory.Build.targets文件
  2. 将您的版本添加到创建的文件中:
  <Project>
    <PropertyGroup>
        <Version>0.1.0-alpha</Version>
    </PropertyGroup>
  </Project>
  1. 就是这样 - 这将应用于解决方案中的所有项目。

您可以使用 Directory.Build.props 和 Directory.Build.targets 做许多其他事情 - 更多内容:( https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build

暂无
暂无

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

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