using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Refl = System.Reflection; using System.Diagnostics; using System.Reflection; namespace Yaulw.Assembly { /// /// Useful Functions for Retrieving General Assembly Information /// public static class AssemblyW { #region Assembly State/Type /// /// Assembly State/Type /// public enum AssemblyST { Calling, Entry, Executing } /// /// Internal Helper Function to Get the Appropriate Assembly State/Type /// /// State/Type of Assembly to Retrieve /// Thrown if invalist AssemblyST is passed in /// Returns the Specified State/Type Assembly private static Refl.Assembly GetAssembly(AssemblyST st) { switch (st) { case AssemblyST.Calling: return Refl.Assembly.GetCallingAssembly(); case AssemblyST.Entry: return Refl.Assembly.GetEntryAssembly(); case AssemblyST.Executing: return Refl.Assembly.GetExecutingAssembly(); } throw (new ArgumentException("Invalid AssemblyST")); } #endregion #region Common Assembly Properties /// /// Returns the Name of the Assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// Returns 'just the Name' of the Assembly public static string GetAssemblyName(AssemblyST st) { string[] curAsmName = GetAssembly(st).FullName.Split(','); return curAsmName[0]; } /// /// Returns the Name of the Assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// Returns 'just the Name' of the Assembly public static string GetAssemblyName(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); string[] curAsmName = asm.FullName.Split(','); return curAsmName[0]; } /// /// Returns the Assembly Title /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// Returns the Assembly Title, as specified in AssemblyInfo public static string GetAssemblyTitle(AssemblyST st) { object[] attributes = GetAssembly(st).GetCustomAttributes(typeof(AssemblyTitleAttribute), false); if (attributes.Length > 0) { AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0]; return titleAttribute.Title; } return String.Empty; } /// /// Returns the Assembly Title /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// Returns the Assembly Title, as specified in AssemblyInfo public static string GetAssemblyTitle(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); object[] attributes = asm.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); if (attributes.Length > 0) { AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0]; return titleAttribute.Title; } return String.Empty; } /// /// Returns the Description of the Assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// Returns the Assembly Description, as specified in AssemblyInfo public static string GetAssemblyDescription(AssemblyST st) { object[] attributes = GetAssembly(st).GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); if (attributes.Length > 0) { AssemblyDescriptionAttribute descriptionAttribute = (AssemblyDescriptionAttribute)attributes[0]; return descriptionAttribute.Description; } return String.Empty; } /// /// Returns the Description of the Assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// Returns the Assembly Description, as specified in AssemblyInfo public static string GetAssemblyDescription(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); object[] attributes = asm.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); if (attributes.Length > 0) { AssemblyDescriptionAttribute descriptionAttribute = (AssemblyDescriptionAttribute)attributes[0]; return descriptionAttribute.Description; } return String.Empty; } /// /// Returns the Company Name of the Assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// Returns the Assembly Company, as specified in AssemblyInfo public static string GetAssemblyCompany(AssemblyST st) { object[] attributes = GetAssembly(st).GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); if (attributes.Length > 0) { AssemblyCompanyAttribute companyAttribute = (AssemblyCompanyAttribute)attributes[0]; return companyAttribute.Company; } return String.Empty; } /// /// Returns the Company Name of the Assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// Returns the Assembly Company, as specified in AssemblyInfo public static string GetAssemblyCompany(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); object[] attributes = asm.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); if (attributes.Length > 0) { AssemblyCompanyAttribute companyAttribute = (AssemblyCompanyAttribute)attributes[0]; return companyAttribute.Company; } return String.Empty; } /// /// Returns the Product Name of the Assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// Returns the Assembly Product, as specified in AssemblyInfo public static string GetAssemblyProductName(AssemblyST st) { object[] attributes = GetAssembly(st).GetCustomAttributes(typeof(AssemblyProductAttribute), false); if (attributes.Length > 0) { AssemblyProductAttribute productAttribute = (AssemblyProductAttribute)attributes[0]; return productAttribute.Product; } return String.Empty; } /// /// Returns the Product Name of the Assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// Returns the Assembly Product, as specified in AssemblyInfo public static string GetAssemblyProductName(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); object[] attributes = asm.GetCustomAttributes(typeof(AssemblyProductAttribute), false); if (attributes.Length > 0) { AssemblyProductAttribute productAttribute = (AssemblyProductAttribute)attributes[0]; return productAttribute.Product; } return String.Empty; } /// /// Returns the Copyright Information of the Assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// Returns the Assembly Copyright, as specified in AssemblyInfo public static string GetAssemblyCopyright(AssemblyST st) { object[] attributes = GetAssembly(st).GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); if (attributes.Length > 0) { AssemblyCopyrightAttribute copyrightAttribute = (AssemblyCopyrightAttribute)attributes[0]; return copyrightAttribute.Copyright; } return String.Empty; } /// /// Returns the Copyright Information of the Assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// Returns the Assembly Copyright, as specified in AssemblyInfo public static string GetAssemblyCopyright(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); object[] attributes = asm.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); if (attributes.Length > 0) { AssemblyCopyrightAttribute copyrightAttribute = (AssemblyCopyrightAttribute)attributes[0]; return copyrightAttribute.Copyright; } return String.Empty; } /// /// Returns the Assembly Version /// /// State/Type of Assembly to Retrieve /// Thrown if invalist AssemblyST is passed in /// Returns the Assembly Version, as specified in AssemblyInfo public static Version GetAssemblyVersion(AssemblyST st) { return SpecializedAssemblyInfo.GetAssemblyNameObj(st).Version; } /// /// Returns the Assembly Version /// /// Assembly to Retrieve Info for /// Thrown if invalist Assembly is passed in /// Returns the Assembly Version, as specified in AssemblyInfo public static Version GetAssemblyVersion(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); return SpecializedAssemblyInfo.GetAssemblyNameObj(asm).Version; } /// /// Returns the Assembly File Version /// /// State/Type of Assembly to Retrieve /// Thrown if invalist AssemblyST is passed in /// Returns the File Version, as specified by the File public static FileVersionInfo GetAssemblyFileVersion(AssemblyST st) { FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(SpecializedAssemblyInfo.GetAssemblyFileNameNPath(st)); return fvi; } /// /// Returns the Assembly File Version /// /// Assembly to Retrieve Info for /// Thrown if invalist Assembly is passed in /// Returns the File Version, as specified by the File public static FileVersionInfo GetAssemblyFileVersion(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(SpecializedAssemblyInfo.GetAssemblyFileNameNPath(asm)); return fvi; } #endregion #region Specialized Assembly Information /// /// Useful Functions for Retrieving Specialized Assembly Information /// public class SpecializedAssemblyInfo { /// /// Returns the Assembly Name Object from the Assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// AssemblyName object for the specified assembly public static Refl.AssemblyName GetAssemblyNameObj(AssemblyST st) { return GetAssembly(st).GetName(); } /// /// Returns the Assembly Name Object from the Assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// AssemblyName object for the specified assembly public static Refl.AssemblyName GetAssemblyNameObj(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); return asm.GetName(); } /// /// Returns the Exact File Name and Path of the Assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// Full File Name and Path public static string GetAssemblyFileNameNPath(AssemblyST st) { Refl.Assembly asm = GetAssembly(st); string curAsmName = asm.Location; return curAsmName; } /// /// Returns the Exact File Name and Path of the Assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// Full File Name and Path public static string GetAssemblyFileNameNPath(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); string curAsmName = asm.Location; return curAsmName; } /// /// Returns the File Name (without Extension) of the Assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// File Name without extension public static string GetAssemblyFileName(AssemblyST st) { string curAsmName = Path.GetFileNameWithoutExtension(GetAssembly(st).ManifestModule.Name); return curAsmName; } /// /// Returns the File Name (without Extension) of the Assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// File Name without extension public static string GetAssemblyFileName(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); string curAsmName = Path.GetFileNameWithoutExtension(asm.ManifestModule.Name); return curAsmName; } /// /// Returns the Path of the Assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// Path public static string GetAssemblyPath(AssemblyST st) { Refl.Assembly asm = GetAssembly(st); string curAsmName = Path.GetDirectoryName(asm.Location); return curAsmName; } /// /// Returns the Path of the Assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// Path public static string GetAssemblyPath(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); string curAsmName = Path.GetDirectoryName(asm.Location); return curAsmName; } /// /// Returns the File Name (with Extension) of the Assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// File Name with extension public static string GetAssemblyFileNameWithExtension(AssemblyST st) { string curAsmName = Path.GetFileName(GetAssembly(st).ManifestModule.Name); return curAsmName; } /// /// Returns the File Name (with Extension) of the Assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// File Name with extension public static string GetAssemblyFileNameWithExtension(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); string curAsmName = Path.GetFileName(asm.ManifestModule.Name); return curAsmName; } /// /// Returns all the Resource names inside the assembly /// /// State/Type of Assembly to Retrieve Info for /// Thrown if invalid AssemblyST is passed in /// Returns all resource names defined in the assembly public static string[] GetAssemblyResourceNames(AssemblyST st) { string[] resources = GetAssembly(st).GetManifestResourceNames(); return resources; } /// /// Returns all the Resource names inside the assembly /// /// Assembly to Retrieve Info for /// Thrown if invalid Assembly is passed in /// Returns all resource names defined in the assembly public static string[] GetAssemblyResourceNames(Refl.Assembly asm) { if (asm == null) throw new ArgumentException("Invalid Assembly"); string[] resources = asm.GetManifestResourceNames(); return resources; } } #endregion } }