GetCustomAttribute() returns null for AssemblyVersionAttribute
Asked Answered
C

3

25

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();
Clarisaclarise answered 17/7, 2009 at 17:1 Comment(1)
If you're in WinForms, you can just use Application.ProductVersionBipinnate
N
21

The AssemblyVersionAttribute is 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 AssemblyFileVersion attribute (i.e. this one is added to the assembly)

There are other attributes that show the same behavior: the AssemblyCultureAttribute and AssemblyFlagsAttribute are 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:

Three attributes, together with a strong name (if applicable), determine the identity of an assembly: name, version, and culture.

Nertie answered 17/7, 2009 at 17:5 Comment(1)
It seems that it is treated differently because it is actually considered a "pseudo custom attribute," not a regular custom attribute.Clarisaclarise
G
9

Your example is not a work-around. It's exactly what the MSDN documentation states you should do, which leads me to believe that code is by-design.
http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.aspx

To get the name [sic] of an assembly you have loaded, call GetName on the assembly to get an AssemblyName, and then get the Version property. To get the name of an assembly you have not loaded, call GetAssemblyName from your client application to check the assembly version that your application uses.

Geilich answered 17/7, 2009 at 17:12 Comment(0)
L
8

Not sure why it behaves this way. Instead of going after the AssemblyVersionAttribute we do this:

Version AssemblyVersion = someAssembly.GetName().Version;

For AssemblyFileVersion we use:

Version fileVersion = new Version("0.0.0.0");
AssemblyFileVersionAttribute[] fileVersionAttributes = (AssemblyFileVersionAttribute[])assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true);
if (fileVersionAttributes != null && fileVersionAttributes.Length > 0) {
    fileVersion = new Version(fileVersionAttributes[0].Version);
}
Litt answered 17/7, 2009 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.