Access AssemblyInfo File and Get Product Informations

Introduction

 
We have very good features in .Net based on the application version and customizations. Basically .Net provide unique assembly file for the common setting within the projects. And we can play with that file within code as well.
 
How I can say that?
 
The .Net provides few attributes based on the assembly manipulations. Suppose we want to know the current version of the product, then we want to access the assembly file and get an answer from that file. To achieve the product version attribute in the assembly file, we must use the AssemblyVersionAttribute attribute. And lots of customization attributes exist in the .Net class library based on the assembly file.
 
List of commonly used attributes.
  1. AssemblyTitleAttribute
  2. AssemblyCompanyAttribute
  3. AssemblyVersionAttribute
  4. AssemblyProductAttribute
  5. AssemblyCopyrightAttribute
  6. AssemblyDescriptionAttribute
This article provides a simple class with commonly using attributes and gets information from the assembly about the product.
 
Let's see the class.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Reflection;  
  5.    
  6. namespace AccessAssembly  
  7. {  
  8.     public class ApplicationDetails  
  9.     {  
  10.         static string companyName = string.Empty;  
  11.         /// Get the name of the system provider name from the assembly ///  
  12.         public static string CompanyName  
  13.         {  
  14.             get  
  15.             {  
  16.                 Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();  
  17.                 if (assembly != null)  
  18.                 {  
  19.                     object[] customAttributes = assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);  
  20.                     if ((customAttributes != null) && (customAttributes.Length > 0))  
  21.                     {  
  22.                         companyName = ((AssemblyCompanyAttribute)customAttributes[0]).Company;  
  23.                     }  
  24.                     if (string.IsNullOrEmpty(companyName))  
  25.                     {  
  26.                         companyName = string.Empty;  
  27.                     }  
  28.                 }  
  29.                 return companyName;  
  30.             }  
  31.         }  
  32.         static string productVersion = string.Empty; ///  
  33.         /// Get System version from the assembly ///  
  34.         public static string ProductVersion  
  35.         {  
  36.             get  
  37.             {  
  38.                 Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();  
  39.                 if (assembly != null)  
  40.                 {  
  41.                     object[] customAttributes = assembly.GetCustomAttributes(typeof(AssemblyVersionAttribute), false);  
  42.                     if ((customAttributes != null) && (customAttributes.Length > 0))  
  43.                     {  
  44.                         productVersion = ((AssemblyVersionAttribute)customAttributes[0]).Version;  
  45.                     }  
  46.                     if (string.IsNullOrEmpty(productVersion))  
  47.                     {  
  48.                         productVersion = string.Empty;  
  49.                     }  
  50.                 }  
  51.                 return productVersion;  
  52.             }  
  53.         }  
  54.         static string productName = string.Empty; ///  
  55.         /// Get the name of the System from the assembly ///  
  56.         public static string ProductName  
  57.         {  
  58.             get  
  59.             {  
  60.                 Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();  
  61.                 if (assembly != null)  
  62.                 {  
  63.                     object[] customAttributes = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);  
  64.                     if ((customAttributes != null) && (customAttributes.Length > 0))  
  65.                     {  
  66.                         productName = ((AssemblyProductAttribute)customAttributes[0]).Product;  
  67.                     }  
  68.                     if (string.IsNullOrEmpty(productName))  
  69.                     {  
  70.                         productName = string.Empty;  
  71.                     }  
  72.                 }  
  73.                 return productName;  
  74.             }  
  75.         }  
  76.         static string copyRightsDetail = string.Empty; ///  
  77.         /// Get the copyRights details from the assembly ///  
  78.         public static string CopyRightsDetail  
  79.         {  
  80.             get  
  81.             {  
  82.                 Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();  
  83.                 if (assembly != null)  
  84.                 {  
  85.                     object[] customAttributes = assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);  
  86.                     if ((customAttributes != null) && (customAttributes.Length > 0))  
  87.                     {  
  88.                         copyRightsDetail = ((AssemblyCopyrightAttribute)customAttributes[0]).Copyright;  
  89.                     }  
  90.                     if (string.IsNullOrEmpty(copyRightsDetail))  
  91.                     {  
  92.                         copyRightsDetail = string.Empty;  
  93.                     }  
  94.                 }  
  95.                 return copyRightsDetail;  
  96.             }  
  97.         }  
  98.         static string productTitle = string.Empty; ///  
  99.         /// Get the Product tile from the assembly ///  
  100.         public static string ProductTitle  
  101.         {  
  102.             get  
  103.             {  
  104.                 Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();  
  105.                 if (assembly != null)  
  106.                 {  
  107.                     object[] customAttributes = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);  
  108.                     if ((customAttributes != null) && (customAttributes.Length > 0))  
  109.                     {  
  110.                         productTitle = ((AssemblyTitleAttribute)customAttributes[0]).Title;  
  111.                     }  
  112.                     if (string.IsNullOrEmpty(productTitle))  
  113.                     {  
  114.                         productTitle = string.Empty;  
  115.                     }  
  116.                 }  
  117.                 return productTitle;  
  118.             }  
  119.         }  
  120.         static string productDescription = string.Empty; ///  
  121.         /// Get the description of the product from the assembly ///  
  122.         public static string ProductDescription  
  123.         {  
  124.             get  
  125.             {  
  126.                 Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();  
  127.                 if (assembly != null)  
  128.                 {  
  129.                     object[] customAttributes = assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);  
  130.                     if ((customAttributes != null) && (customAttributes.Length > 0))  
  131.                     {  
  132.                         productDescription = ((AssemblyDescriptionAttribute)customAttributes[0]).Description;  
  133.                     }  
  134.                     if (string.IsNullOrEmpty(productDescription))  
  135.                     {  
  136.                         productDescription = string.Empty;  
  137.                     }  
  138.                 }  
  139.                 return productDescription;  
  140.             }  
  141.         }  
  142.     }  

Let us see how to get a product description from the assembly file
  1. public static string ProductDescription  
  2. {  
  3.     get  
  4.     {  
  5.         //get the entry assebly file for the product  
  6.         Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();  
  7.         if (assembly != null)  
  8.         {  
  9.             //now get the customattribute for the AssemblyDescriptionAttribute  
  10.             object[] customAttributes = assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);  
  11.             if ((customAttributes != null) && (customAttributes.Length > 0))  
  12.             {  
  13.                 productDescription = ((AssemblyDescriptionAttribute)customAttributes[0]).Description;  
  14.             }  
  15.             if (string.IsNullOrEmpty(productDescription))  
  16.             {  
  17.                 productDescription = string.Empty;  
  18.             }  
  19.         }  
  20.         return productDescription;  
  21.     }  

We want to get all custom attribute from the assembly file based on the AssemblyDescriptionAttribute.
  1. object[] customAttributes = assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);  
After this, access the first attribute in the collections, and get the value from that
  1. productDescription = ((AssemblyDescriptionAttribute)customAttributes[0]).Description; 

Conclusion

 
The .Net provides good customization based on the assembly file. So please try to access all other attributes in the assembly file and enjoy.


Recommended Ebook

.NET Interview Questions and Answer: Practical Implementation

Download Now!
Similar Articles