Reading manifest from assembly

By Mirek on (tags: assembly information, manifest, categories: code)

Information like product name, file version, copyright, etc are stored in the assembly manifest. In this post I would like to present how these informations can be read from an assembly and showed in the UI.

Assembly manifest can be edited by choosing Properties on Visual Studio project and pressing button Assembly Information

manifest

All we need to extract these data programmatically is following class

 

   1: public class AssemblyInformation
   2:     {
   3:         public string Company { get; set; }
   4:  
   5:         public string Copyright { get; set; }
   6:  
   7:         public string Description { get; set; }
   8:  
   9:         public string ProductName { get; set; }
  10:  
  11:         public string Title { get; set; }
  12:  
  13:         public string Version { get; set; }
  14:  
  15:         public string FileVersion { get; set; }
  16:  
  17:         public string Culture { get; set; }
  18:  
  19:         public string InformationalVersion { get; set; }
  20:  
  21:         public string AssemblyName { get; set; }
  22:  
  23:         public string AssemblyTrademark { get; set; }
  24:  
  25:         /// <summary>
  26:         /// Create and initialize AssemblyInformation object
  27:         /// </summary>
  28:         /// <param name="assembly">Assambly which the information will be retreived from.
  29:         /// By default calling assembly is used</param>
  30:         public AssemblyInformation(Assembly assembly = null)
  31:         {
  32:             if (assembly == null)
  33:                 assembly = Assembly.GetCallingAssembly();
  34:  
  35:             object[] attribs = assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
  36:             Company = attribs.Length == 0 ? "" : ((AssemblyCompanyAttribute)attribs[0]).Company;
  37:  
  38:             attribs = assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
  39:             Copyright = attribs.Length == 0 ? "" : ((AssemblyCopyrightAttribute)attribs[0]).Copyright;
  40:  
  41:             attribs = assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
  42:             Description = attribs.Length == 0 ? "" : ((AssemblyDescriptionAttribute)attribs[0]).Description;
  43:  
  44:             attribs = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
  45:             ProductName = attribs.Length == 0 ? "" : ((AssemblyProductAttribute)attribs[0]).Product;
  46:  
  47:             attribs = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
  48:             Title = attribs.Length == 0 ? "" : ((AssemblyTitleAttribute)attribs[0]).Title;
  49:  
  50:             attribs = assembly.GetCustomAttributes(typeof(AssemblyVersionAttribute), false);
  51:             Version = attribs.Length == 0 ? "" : ((AssemblyVersionAttribute)attribs[0]).Version;
  52:  
  53:             attribs = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
  54:             FileVersion = attribs.Length == 0 ? "" : ((AssemblyFileVersionAttribute)attribs[0]).Version;
  55:  
  56:             attribs = assembly.GetCustomAttributes(typeof(AssemblyCultureAttribute), false);
  57:             Culture = attribs.Length == 0 ? "" : ((AssemblyCultureAttribute)attribs[0]).Culture;
  58:             
  59:             attribs = assembly.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
  60:             InformationalVersion = attribs.Length == 0 ? "" : ((AssemblyInformationalVersionAttribute)attribs[0]).InformationalVersion;
  61:             
  62:             attribs = assembly.GetCustomAttributes(typeof(AssemblyName), false);
  63:             AssemblyName = attribs.Length == 0 ? "" : ((AssemblyName)attribs[0]).Name;
  64:             
  65:             attribs = assembly.GetCustomAttributes(typeof(AssemblyTrademarkAttribute), false);
  66:             AssemblyTrademark = attribs.Length == 0 ? "" : ((AssemblyTrademarkAttribute)attribs[0]).Trademark;
  67:  
  68:         }
  69:     }

 

Each manifest information is represented by particular assembly attribute. These attributes can be found in System.Reflection namespace. The reflection is used here to retrieve each attribute value from an assembly.

The simplest usage:

 

   1: AssemblyInformation assemblyData = new AssemblyInformation();

 

will collect manifest from the calling assembly (ex. console application). Then you can used properties of assemblyData which are set with manifest details.