AssemblyExt.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace Quadarax.Foundation.Core.Reflection.Extensions
  7. {
  8. public static class AssemblyExt
  9. {
  10. #region *** Private Fields ***
  11. #endregion
  12. #region *** Public Properties ***
  13. #endregion
  14. #region *** Constructors ***
  15. #endregion
  16. #region *** Public operations & overrides ***
  17. public static Version? GetVersion(this Assembly oAssembly)
  18. {
  19. if (oAssembly == null)
  20. throw new ArgumentNullException(nameof(oAssembly));
  21. return oAssembly.GetName().Version;
  22. }
  23. public static string GetDescription(this Assembly oAssembly)
  24. {
  25. var oAttribute = oAssembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), true).FirstOrDefault();
  26. if (oAttribute == null)
  27. return string.Empty;
  28. return ((AssemblyDescriptionAttribute)oAttribute).Description;
  29. }
  30. public static string GetProduct(this Assembly oAssembly)
  31. {
  32. var oAttribute = oAssembly.GetCustomAttributes(typeof(AssemblyProductAttribute), true).FirstOrDefault();
  33. if (oAttribute == null)
  34. return string.Empty;
  35. return ((AssemblyProductAttribute)oAttribute).Product;
  36. }
  37. public static string GetCompany(this Assembly oAssembly)
  38. {
  39. var oAttribute = oAssembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true).FirstOrDefault();
  40. if (oAttribute == null)
  41. return string.Empty;
  42. return ((AssemblyCompanyAttribute)oAttribute).Company;
  43. }
  44. public static string GetCopyright(this Assembly oAssembly)
  45. {
  46. var oAttribute = oAssembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), true).FirstOrDefault();
  47. if (oAttribute == null)
  48. return string.Empty;
  49. return ((AssemblyCopyrightAttribute)oAttribute).Copyright;
  50. }
  51. public static string GetTrademark(this Assembly oAssembly)
  52. {
  53. var oAttribute = oAssembly.GetCustomAttributes(typeof(AssemblyTrademarkAttribute), true).FirstOrDefault();
  54. if (oAttribute == null)
  55. return string.Empty;
  56. return ((AssemblyTrademarkAttribute)oAttribute).Trademark;
  57. }
  58. public static string GetAssemblyPath(this Assembly oAssembly)
  59. {
  60. if (oAssembly == null)
  61. throw new ArgumentNullException(nameof(oAssembly));
  62. return Path.GetDirectoryName(new Uri(oAssembly.Location)?.LocalPath) + Path.DirectorySeparatorChar;
  63. }
  64. public static IEnumerable<Type> GetTypesWithAttribute<TAttribute>(this Assembly oAssembly, bool bInherit)where TAttribute : System.Attribute
  65. {
  66. var types = oAssembly.GetTypes();
  67. return types.Where(x => x.IsDefined(typeof(TAttribute), bInherit)).ToArray();
  68. }
  69. #endregion
  70. #region *** Private operations & overrides ***
  71. #endregion
  72. }
  73. }