[英]How do push 2 projects in the same solution to my Github repository?
[英]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
文件为了这。
Directory.Build.targets
文件 <Project>
<PropertyGroup>
<Version>0.1.0-alpha</Version>
</PropertyGroup>
</Project>
您可以使用 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.