AssemblyInfo.cs
文件是 .NET 项目中用于定义程序集元数据的文件。这些元数据包括版本号、文化信息、版权声明等。在.NET Core和.NET 5/6/7等较新的框架中,这个文件不再是必需的,因为可以通过项目文件(.csproj
)来设置这些属性,但它在传统的.NET Framework项目中仍然很常见。
在AssemblyInfo.cs
中更新值的常见做法包括:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 程序集的版本信息由下面四个值组成:
//
// 主版本
// 次版本
// 内部版本号
// 修订号
//
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
// 程序集的名称
[assembly: AssemblyTitle("Your Application Title")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Your Company")]
[assembly: AssemblyProduct("Your Product")]
[assembly: AssemblyCopyright("Copyright © Your Company 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// ComVisible 设置为 false 使此程序集中的类型对 COM 组件不可见。
// 如果需要从 COM 访问此程序集中的某个类型,请针对该类型将 ComVisible 属性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("your-guid-here")]
// 程序集版本信息
[assembly: AssemblyInformationalVersion("1.0.0")]
问题:版本号更新不及时,导致发布管理混乱。
解决方法:
.csproj
)中使用MSBuild任务来更新版本号。例如,在.csproj
中添加以下脚本:
<PropertyGroup>
<Version>1.0.0</Version>
<FileVersion>$(Version).0</FileVersion>
<AssemblyVersion>$(Version).0.0</AssemblyVersion>
</PropertyGroup>
<Target Name="UpdateAssemblyInfo" BeforeTargets="PrepareForBuild">
<AssemblyInfo CodeLanguage="CS" OutputFile="Properties\AssemblyInfo.cs"
AssemblyVersion="$(AssemblyVersion)" AssemblyFileVersion="$(FileVersion)" />
</Target>
这样,每次构建项目时,版本号都会自动更新。
假设你想在每次发布时自动增加修订号,可以在.csproj
中设置:
<PropertyGroup>
<Version>1.0.0.*</Version>
</PropertyGroup>
这将使.NET框架自动增加修订号,基于当前日期和时间。
通过这种方式,你可以确保每次发布都有唯一的版本号,同时减少了手动更新版本号的错误和工作量。
领取专属 10元无门槛券
手把手带您无忧上云