Advertisement
EmilySamantha80

Assembly information

Mar 7th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.83 KB | None | 0 0
  1. // Title:  Assembly information
  2. // Author: Emily Heiner
  3. // This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported license.
  4. // You are free to share and adapt this code for any purposes, commerical and non-commercial,
  5. // as long as appropriate credit is given, you indicate if changes were made, and you share your
  6. // modified code. License details can be found at https://creativecommons.org/licenses/by-sa/3.0/
  7.  
  8. using System;
  9. using System.Reflection;
  10.  
  11. namespace ESH.Utility
  12. {
  13.     public static class AssemblyInfo
  14.     {
  15.         #region Assembly Attribute Accessors
  16.  
  17.         //Gets the entry assembly for web apps
  18.         //http://stackoverflow.com/questions/4277692/getentryassembly-for-web-applications
  19.         //Note: On App_Start System.Web.HttpContext.Current.ApplicationInstance is NULL
  20.         private static Assembly GetWebEntryAssembly()
  21.         {
  22.             if (System.Web.HttpContext.Current == null || System.Web.HttpContext.Current.ApplicationInstance == null)
  23.             {
  24.                 return null;
  25.             }
  26.             var type = System.Web.HttpContext.Current.ApplicationInstance.GetType();
  27.             while (type != null && type.Namespace == "ASP")
  28.             {
  29.                 type = type.BaseType;
  30.             }
  31.             return type == null ? null : type.Assembly;
  32.         }
  33.  
  34.         public static string GetCallingAssemblyPath()
  35.         {
  36.             var path = Path.GetDirectoryName(new Uri(AssemblyInfo.GetCallingAssembly().CodeBase).LocalPath);
  37.             return path;
  38.         }
  39.  
  40.         //Try as hard as possible to get the proper entry assembly
  41.         public static Assembly GetCallingAssembly()
  42.         {
  43.             Assembly assembly = null;
  44.             assembly = GetWebEntryAssembly();
  45.             if (assembly == null)
  46.                 assembly = Assembly.GetEntryAssembly();
  47.             if (assembly == null)
  48.                 assembly = Assembly.GetCallingAssembly();
  49.             if (assembly == null)
  50.                 assembly = Assembly.GetExecutingAssembly();
  51.  
  52.             return assembly;
  53.         }
  54.  
  55.         public static string AssemblyFileName
  56.         {
  57.             get
  58.             {
  59.                 string location = GetCallingAssembly().Location;
  60.                 return location.Substring(location.LastIndexOf(@"\") + 1);
  61.             }
  62.         }
  63.  
  64.         public static string AssemblyLocation
  65.         {
  66.             get
  67.             {
  68.                 //Assembly.Location is not always accurate, especially when used in Web Applications and NUnit. Assembly.CodeBase is, but must be manipulated.
  69.                 //http://stackoverflow.com/questions/52797/how-do-i-get-the-path-of-the-assembly-the-code-is-in
  70.                 //The Assembly.Location property sometimes gives you some funny results when using NUnit (where assemblies run from a temporary folder), so I prefer to use CodeBase
  71.                 //which gives you the path in URI format, then UriBuild.UnescapeDataString removes the File:// at the beginning, and GetDirectoryName changes it to the normal windows format.
  72.                 string codeBase = GetCallingAssembly().CodeBase;
  73.                 UriBuilder uri = new UriBuilder(codeBase);
  74.                 string path = Uri.UnescapeDataString(uri.Path);
  75.                 return Path.GetDirectoryName(path);
  76.             }
  77.         }
  78.  
  79.         public static string AssemblyTitle
  80.         {
  81.             get
  82.             {
  83.                 object[] attributes = GetCallingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
  84.                 if (attributes.Length > 0)
  85.                 {
  86.                     AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
  87.                     if (titleAttribute.Title != "")
  88.                     {
  89.                         return titleAttribute.Title;
  90.                     }
  91.                 }
  92.                 return String.Empty;
  93.             }
  94.         }
  95.  
  96.         public static string AssemblyVersion
  97.         {
  98.             get
  99.             {
  100.                 return GetCallingAssembly().GetName().Version.ToString();
  101.             }
  102.         }
  103.  
  104.         public static string AssemblyDescription
  105.         {
  106.             get
  107.             {
  108.                 object[] attributes = GetCallingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
  109.                 if (attributes.Length == 0)
  110.                 {
  111.                     return "";
  112.                 }
  113.                 return ((AssemblyDescriptionAttribute)attributes[0]).Description;
  114.             }
  115.         }
  116.  
  117.         public static string AssemblyProduct
  118.         {
  119.             get
  120.             {
  121.                 object[] attributes = GetCallingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
  122.                 if (attributes.Length == 0)
  123.                 {
  124.                     return "";
  125.                 }
  126.                 return ((AssemblyProductAttribute)attributes[0]).Product;
  127.             }
  128.         }
  129.  
  130.         public static string AssemblyCopyright
  131.         {
  132.             get
  133.             {
  134.                 object[] attributes = GetCallingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
  135.                 if (attributes.Length == 0)
  136.                 {
  137.                     return "";
  138.                 }
  139.                 return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
  140.             }
  141.         }
  142.  
  143.         public static string AssemblyCompany
  144.         {
  145.             get
  146.             {
  147.                 object[] attributes = GetCallingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
  148.                 if (attributes.Length == 0)
  149.                 {
  150.                     return "";
  151.                 }
  152.                 return ((AssemblyCompanyAttribute)attributes[0]).Company;
  153.             }
  154.         }
  155.         #endregion
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement