我开发了一个nuget包,需要获取入口程序集的包版本。到目前为止,我都是用这个方法来获取版本的:
using System.Reflection;
var version = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
Console.WriteLine(version);
它运行良好,到目前为止仅返回入口程序集的包版本。但 InformationalVersion
1.2.3-alpha
,而是得到
1.2.3-alpha+9d1761b41223159ec45d7d492e08820f706d7ad1
。我知道可以按照此处的建议在条目程序集的 csproj 中添加
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
:FileVersionInfo.ProductVersion 突然包含 git commit hash 但是这个解决方案不适合我,因为我的包被多个客户端使用,我不想修改所有客户端。我需要一种方法来仅获取包版本而不添加 SourceRevisionId。
是否有正确的方法来执行此操作,或者唯一的解决方案是截断“+”号后的字符串?
var version = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
version = version.Split("+").First();
Console.WriteLine(version);
但是,我确实质疑为什么您不能更改 .csproj - 您应该能够更改它,然后将更新的包推送给您的客户端,而无需对每个客户端进行任何手动更新。