Get the .NET assembly's AssemblyInformationalVersion value?

What is the C# syntax for getting the assembly's AssemblyInformationalVersion attribute value at runtime? Example:

[assembly: AssemblyInformationalVersion("1.2.3.4")]


Solution 1:

using System.Reflection.Assembly  
using System.Diagnostics.FileVersionInfo

// ...

public string GetInformationalVersion(Assembly assembly) {
    return FileVersionInfo.GetVersionInfo(assembly.Location).ProductVersion;
}

Solution 2:

var attr = Assembly
    .GetEntryAssembly()
    .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false) 
    as AssemblyInformationalVersionAttribute[];

It's an array of AssemblyInformationalVersionAttribute. It isn't ever null even if there are no attribute of the searched type.

var attr2 = Attribute
    .GetCustomAttribute(
        Assembly.GetEntryAssembly(), 
        typeof(AssemblyInformationalVersionAttribute)) 
    as AssemblyInformationalVersionAttribute;

This can be null if the attribute isn't present.

var attr3 = Attribute
    .GetCustomAttributes(
         Assembly.GetEntryAssembly(), 
         typeof(AssemblyInformationalVersionAttribute)) 
    as AssemblyInformationalVersionAttribute[];

Same as first.