.NET Core/Standard 自动版本控制和AssemblyInfo自定义

.NET Core或.NET Standard项目中,经常需要对项目进行版本控制。本文将介绍如何通过自定义AssemblyInfo文件来实现自动版本控制,以及如何手动管理AssemblyInfo属性。

自动生成AssemblyInfo属性

.NET Core或.NET Standard项目中,当创建一个标准项目时,会自动生成一组属性,这些属性基于项目的设置。这些属性可以在obj文件夹中找到,其中会创建一个自动生成的xxxxAssemblyInfo.cs类。

例如,以下是一个自动生成的AssemblyInfo.cs文件的示例:

using System; using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("SomeDemoCode.Std")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")] [assembly: System.Reflection.AssemblyProductAttribute("SomeDemoCode.Std")] [assembly: System.Reflection.AssemblyTitleAttribute("SomeDemoCode.Std")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] // Generated by the MSBuild WriteCodeFragment class.

当项目构建时,这些版本信息将被应用到项目输出中。

.NET Core或.NET Standard并没有内置的自动版本递增功能,但可以通过一些方法来实现。一种常见的方法是在.csproj文件中定义版本后缀。

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <Platforms>x64</Platforms> <DefineConstants>STD</DefineConstants> <PlatformTarget>x64</PlatformTarget> <AssemblyName>SomeDemoCode.Std</AssemblyName> <RootNamespace>SomeDemoCode.Std</RootNamespace> <VersionSuffix>1.0.0.$([System.DateTime]::UtcNow.ToString("mmff"))</VersionSuffix> <AssemblyVersion Condition="'$(VersionSuffix)' == ''">0.0.0.1</AssemblyVersion> <AssemblyVersion Condition="'$(VersionSuffix)' != ''">$(VersionSuffix)</AssemblyVersion> <Version Condition="'$(VersionSuffix)' == ''">0.0.1.0</Version> <Version Condition="'$(VersionSuffix)' != ''">$(VersionSuffix)</Version> <Company>SAS</Company> <Authors>SAS</Authors> <Copyright>Copyright © SAS 2020</Copyright> <Product>Demo 1.0</Product> </PropertyGroup> </Project>

通过这种方式,构建项目时会自动应用版本后缀。

InternalsVisibleTo的使用

有时希望将.NET Standard项目暴露给测试项目。如果.NET Core/Standard项目基于默认值或.csproj文件中的属性自动生成AssemblyInfo,那么如何添加InternalsVisibleTo属性呢?

幸运的是,这很简单。只需要在自定义文件中添加以下内容:

using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("SomeDemoCode.IntegrationTests")]

这样,就可以在构建时自动生成版本号,并允许测试项目访问内部成员。

如果想使用.NET Framework的自动版本控制方法,通常在AssemblyInfo.cs文件中使用通配符。

[assembly: AssemblyVersion("1.0.*")]

但是,如果尝试通过添加自己的AssemblyInfo.cs来覆盖它,这将不起作用。构建时会出现错误。

为了避免这个问题,可以在.NET Core/.NET Standard的csproj文件中添加以下内容:

<PropertyGroup> <GenerateAssemblyInfo>false</GenerateAssemblyInfo> <Deterministic>false</Deterministic> </PropertyGroup>

这样,就可以包含一个自定义的AssemblyInfo.cs文件,其中可以使用自动递增的版本号。

using System.Reflection; using System.Runtime.InteropServices; [assembly: AssemblyTitle("SomeDemoCode.Std")] [assembly: AssemblyDescription("SomeDemoCode .NET Standard")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("SAS")] [assembly: AssemblyProduct("SAS 1.0")] [assembly: AssemblyCopyright("Copyright © SAS 2020")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("CA7543D7-0F0F-4B48-9398-2712098E9324")] [assembly: AssemblyVersion("1.0.*")]
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485