assemblyinfo.cs文件具有AssemblyVersion属性,但是当我运行以下命令时:
Attribute[] y = Assembly.GetExecutingAssembly().GetCustomAttributes();
我明白了:
System.Runtime.InteropServices.ComVisibleAttribute System.Runtime.CompilerServices.RuntimeCompatibilityAttribute System.Runtime.CompilerServices.CompilationRelaxationsAttribute System.Runtime.InteropServices.GuidAttribute System.Diagnostics.DebuggableAttribute System.Reflection.AssemblyTrademarkAttribute System.Reflection.AssemblyCopyrightAttribute System.Reflection.AssemblyCompanyAttribute System.Reflection.AssemblyConfigurationAttribute System.Reflection.AssemblyFileVersionAttribute System.Reflection.AssemblyProductAttribute System.Reflection.AssemblyDescriptionAttribute
[assembly: AssemblyVersion("5.5.5.5")]
…如果我尝试直接访问它,我会得到一个例外:
Attribute x = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(),typeof(AssemblyVersionAttribute)); //exception
我想我将无法使用该属性,但是.NET怎么没有读它呢?
解决方法
如果您正试图获得程序集版本,那么它非常简单:
Console.WriteLine("The version of the currently executing assembly is: {0}",Assembly.GetExecutingAssembly().GetName().Version);
该属性的类型为System.Version,具有Major,Minor,Build和Revision属性.
例如.版本1.2.3.4的程序集具有:
>重大= 1>次要= 2> Build = 3>修订版= 4