I’m adding an About dialog to my .NET application and I’m querying the assembly’s attributes for information to display. When I attempt to retrieve my assembly’s AssemblyVersionAttribute using GetCustomAttribute() it returns null:
// Works fine
AssemblyTitleAttribute title
= (AssemblyTitleAttribute)Attribute.GetCustomAttribute(
someAssembly, typeof(AssemblyTitleAttribute));
// Gets null
AssemblyVersionAttribute version
= (AssemblyVersionAttribute)Attribute.GetCustomAttribute(
someAssembly, typeof(AssemblyVersionAttribute));
My AssemblyInfo.cs seems fine. I have these attributes defined:
[assembly: AssemblyTitle("Some Application")]
[assembly: AssemblyVersion("1.0.0.0")]
What’s the deal? I do have a workaround, but I would like to know why the above code doesn’t work.
// Work-around
string version = someAssembly.GetName().Version.ToString();
The
AssemblyVersionAttributeis not added to the assembly, but is treated in a “special” way by the compiler (i.e. it sets the version of the assembly)You CAN get the
AssemblyFileVersionattribute (i.e. this one is added to the assembly)There are other attributes that show the same behavior: the
AssemblyCultureAttributeandAssemblyFlagsAttributeare also used for setting assembly properties, and are not added to the assembly as custom attributes.All of these attributes are listed under the Assembly Identity Attributes in the documentation. The documentation says this about these attributes: