Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why can't I load the AssemblyVersion attribute using reflection?

The assemblyinfo.cs file has the AssemblyVersion attribute, but when I run the following:

Attribute[] y = Assembly.GetExecutingAssembly().GetCustomAttributes();

I get:

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

and yet I have checked countless times that this attribute is present in my code:

 [assembly: AssemblyVersion("5.5.5.5")]

...and if I try to access it directly I get an exception:

Attribute x = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyVersionAttribute)); //exception

I guess I won't be able to use that attribute, but how come .NET isn't reading it?

like image 834
John Smith Avatar asked Feb 14 '13 02:02

John Smith


1 Answers

If you're trying to just get the assembly version, it's pretty straight forward:

Console.WriteLine("The version of the currently executing assembly is: {0}", Assembly.GetExecutingAssembly().GetName().Version);

The property is a type of System.Version, which has Major, Minor, Build, and Revision properties.

Eg. an assembly of version 1.2.3.4 has:

  • Major = 1
  • Minor = 2
  • Build = 3
  • Revision = 4
like image 118
ashes999 Avatar answered Nov 10 '22 01:11

ashes999