【发布时间】:2011-12-07 21:02:35
【问题描述】:
在运行时获取程序集的 AssemblyInformationalVersion 属性值的 C# 语法是什么?示例:
[assembly: AssemblyInformationalVersion("1.2.3.4")]
【问题讨论】:
在运行时获取程序集的 AssemblyInformationalVersion 属性值的 C# 语法是什么?示例:
[assembly: AssemblyInformationalVersion("1.2.3.4")]
【问题讨论】:
using System.Reflection.Assembly
using System.Diagnostics.FileVersionInfo
// ...
public string GetInformationalVersion(Assembly assembly) {
return FileVersionInfo.GetVersionInfo(assembly.Location).ProductVersion;
}
【讨论】:
Assembly.Load(byte[]) 加载,则可能是这种情况
mkbundle'd 你的 Mono 应用程序也不起作用
var attr = Assembly
.GetEntryAssembly()
.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)
as AssemblyInformationalVersionAttribute[];
这是一个AssemblyInformationalVersionAttribute 的数组。即使没有搜索到的类型的属性,它也永远不会为空。
var attr2 = Attribute
.GetCustomAttribute(
Assembly.GetEntryAssembly(),
typeof(AssemblyInformationalVersionAttribute))
as AssemblyInformationalVersionAttribute;
如果属性不存在,则可以为 null。
var attr3 = Attribute
.GetCustomAttributes(
Assembly.GetEntryAssembly(),
typeof(AssemblyInformationalVersionAttribute))
as AssemblyInformationalVersionAttribute[];
和第一个一样。
【讨论】:
GetCustomAttribute 而不是GetCustomAttributes。
Attribute的静态方法,不能使用Assembly的实例方法
Assembly.GetEntryAssembly() 应替换为 Assembly.GetExecutingAssembly() 或 Assembly.GetCallingAssembly()。如果程序集是插件,通常这是必需的 - 在这种情况下,GetEntryAssembly() 将返回主机应用程序程序集。
在您的应用程序中使用已知类型,您可以简单地执行以下操作:
using System.Reflection;
public static readonly string ProductVersion = typeof(MyKnownType).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
当然,您用于获取应用属性的程序集的任何过程都是好的。请注意,这不依赖于 System.Diagnostics 或 WinForm 的 Application 对象。
【讨论】:
即使问题有点老了:
我提出了一个适合我的不同解决方案:
Application.ProductVersion
【讨论】:
AssemblyInformationalVersionAttribute attribute =
(AssemblyInformationalVersionAttribute)Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false).FirstOrDefault();
if (attribute != null)
Console.WriteLine(attribute.InformationalVersion);
【讨论】:
public static string GetInformationalVersion() =>
Assembly
.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
虽然我的回答与其他一些人相似,但我认为它有一些优势:
GetEntryAssembly() 替换为GetExecutingAssembly()
GetCustomAttribute<T>,并且认为这个变体更具可读性。另请参阅Microsoft Docs on GetCustomAttribute<T>(Assembly)。
【讨论】:
补充 lance 的回答:您可以使用 Application.ResourceAssembly.Location 找出程序集的文件路径。这样就可以在一行中获取 AssemblyInformationalVersion 字符串
System.Diagnostics.FileVersionInfo.GetVersionInfo(Application.ResourceAssembly.Location).ProductVersion
【讨论】:
http://msdn.microsoft.com/en-us/library/system.reflection.assemblyinformationalversionattribute.aspx
查看 InformationalVersion 属性
【讨论】:
AssemblyVersionAttribute定义的版本,而不是AssemblyInformationalVersionAttribute。
基于@Aerthal 的回答,如果您想要一个从 MVC Razor 视图中获取 AssemblyInformationalVersionAttribute 的衬垫:
@System.Diagnostics.FileVersionInfo.GetVersionInfo(typeof(Zeroarc.Candid.Web.MvcApplication).Assembly.Location).ProductVersion
【讨论】:
鉴于从 PE 标头中检索日期可能不够可靠,有一种方法可以在您的 AssemblyInfo.cs 中包含其他属性
[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("1.0.0")]
// and this:
[assembly: AssemblyInformationalVersion("1.0.0 (Build Date: 14.07.2020)")]
字符串应该是可读的,因为它对最终用户可见。但是,如果您坚持使用特定格式,则可以轻松可靠性对其进行解析。
注意:我们使用的是 Jenkins 构建服务器,它将版本信息连同日期字符串一起写入 AssemblyInfo.cs。
【讨论】: