目录
使用自动生成的.NET Core/Standard AssemblyInfo.cs
但是InternalsVisibleTo呢?
选择退出自动装配信息生成过程并使用我们自己的自动增量方案
使用自动生成的.NET Core/Standard AssemblyInfo.cs当您创建一个新的.NET Core/.NET Standard项目时,您将获得一组自动生成的属性,这些属性基于项目的这些设置。
实际上,可以在obj文件夹中为您创建一个自动生成的xxxxAssemblyInfo.cs类。
/------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
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.
因此,当我们构建项目时,最终将这个版本应用于项目的结果输出。
好的,这说明了AssemblyInfo
资料如何在.NET Core/Standard中工作,但是.NET Core/Standard执行自动递增版本的方式是什么?
- https://stackoverflow.com/questions/43019832/auto-versioning-in-visual-studio-2017-net-core
- https://andrewlock.net/version-vs-versionsuffix-vs-packageversion-what-do-they-all-mean/
读完所有这些内容后,最好的方法似乎是基于坚持使用自动生成的Assembly.info资料,但想出一些方案来帮助在构建时生成程序集版本。
这意味着您要确保您的.csproj文件看起来像这样,可以看出其中一些.NET Core/Standard自动生成的AssemblyInfo
资料可以直接在项目文件中使用。
netstandard2.0
x64
STD
x64
SomeDemoCode.Std
SomeDemoCode.Std
1.0.0.$([System.DateTime]::UtcNow.ToString(mmff))
0.0.0.1
$(VersionSuffix)
0.0.1.0
$(VersionSuffix)
SAS
SAS
Copyright © SAS 2020
Demo 1.0
有了这个,您将只使用.NET Core/Standard方法就可以获得自动版本化的Assembly版本
通常,我们仍然希望将.NET Standard项目公开给测试项目。而且,如果.NET Core/Standard项目基于默认的值或实际.csproj文件中的属性自动生成AssemblyInfo
,那么我们如何添加InternalsVisibleTo,为.NET Core/Standard项目自动创建的AssemblyInfo
似乎没有涵盖这一点,它似乎也不能作为csproj级别的MSBUILD
属性项使用。那么我们该怎么做呢?
幸运的是,这非常简单,我们只需要在一个自定义文件中执行以下操作,即可调用所需的任何文件,如果需要,甚至可以将其称为“AssemblyInfo.cs”
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("SomeDemoCode.IntegrationTests")]
选择退出自动装配信息生成过程并使用我们自己的自动增量方案
如果您想使用.NET Framework方法进行自动版本控制,通常可以使用
[assembly: AssemblyVersion("1.0.*")]
因此,您可能会想,嗯,所以我可以通过添加自己的AssemblyInfo.cs来重写它,但这将无法正常工作,您在构建时会得到此信息
> Error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute
> Error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute
> Error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute
幸运的是,我们可以选择退出此自动生成过程,在此过程中,我们必须将以下内容添加到我们的.NET Core/.NET标准csproj文件中。
false
false
您需要确定性属性,否则将收到此错误
Wildcards are only allowed if the build is not deterministic,
which is the default for .Net Core projects. Adding False to csproj fixes the issue.
有了这个,我们可以包括一个自定义AssemblyInfo.cs,它可以使用自动递增的版本号,在这里,我们在指定AssemblyVersion时使用通配符
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[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("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("CA7543D7-0F0F-4B48-9398-2712098E9324")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.*")]
现在,当我们构建时,您将在.NET Core/Standard项目中获得一些不错的自动递增程序集版本号。