QualifiedName.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using Quadarax.Foundation.Core.Reflection.Extensions;
  5. namespace Quadarax.Foundation.Core.Reflection
  6. {
  7. public class QualifiedName
  8. {
  9. #region *** Constants ***
  10. private const char CC_SEPARATOR = ',';
  11. #endregion
  12. #region *** Public Properties ***
  13. // ReSharper disable MemberCanBePrivate.Global
  14. // ReSharper disable UnusedAutoPropertyAccessor.Global
  15. public string Name { get; private set; }
  16. public string? Assembly { get; private set; }
  17. public string? Culture { get; private set; }
  18. public string Token { get; private set; }
  19. public string Version { get; private set; }
  20. public string AssemblyFullName { get; private set; }
  21. // ReSharper restore UnusedAutoPropertyAccessor.Global
  22. // ReSharper restore MemberCanBePrivate.Global
  23. #endregion
  24. #region *** Constructors ***
  25. public QualifiedName(string sReflectionQualifiedName)
  26. {
  27. Name = string.Empty;
  28. Token = string.Empty;
  29. Version = string.Empty;
  30. AssemblyFullName = string.Empty;
  31. var aParts = sReflectionQualifiedName.Split(CC_SEPARATOR)
  32. .Select(x => x.Trim())
  33. .ToList();
  34. if (aParts.Count==0)
  35. return;
  36. if (aParts.Count==1)
  37. {
  38. //jedna se o bud o codebase nebo assembly
  39. bool bCodebase = false;
  40. try
  41. {
  42. var oType = Type.GetType(aParts[0].Trim(), true);
  43. if (oType == null)
  44. throw new Exception("Type is null");
  45. SetupProperties(oType.Assembly.GetName());
  46. bCodebase = true;
  47. }
  48. catch{}
  49. if (bCodebase)
  50. {
  51. //codebase
  52. Name = aParts[0].Trim();
  53. }
  54. else
  55. {
  56. //assembly
  57. try
  58. {
  59. SetupProperties(new AssemblyName(aParts[0].Trim()));
  60. }
  61. catch (System.Exception e)
  62. {
  63. ThrowCannotParse(sReflectionQualifiedName, e);
  64. }
  65. }
  66. }
  67. if (aParts.Count > 1)
  68. {
  69. //jedna se bud o codebase, assembly nebo assembly
  70. try
  71. {
  72. if (aParts[1].IndexOf('=') > 0)
  73. {
  74. //assembly
  75. SetupProperties(new AssemblyName(string.Join(CC_SEPARATOR.ToString() , aParts)));
  76. }
  77. else
  78. {
  79. //codebase, assembly
  80. SetupProperties(new AssemblyName(string.Join(CC_SEPARATOR.ToString(), aParts.Skip(1))));
  81. Name = aParts[0].Trim();
  82. }
  83. }
  84. catch (System.Exception e)
  85. {
  86. ThrowCannotParse(sReflectionQualifiedName, e);
  87. }
  88. }
  89. }
  90. #endregion
  91. #region *** Public Operations ***
  92. public string GetQualifiedNameWithoutVersion()
  93. {
  94. return Name + CC_SEPARATOR + " " + Assembly;
  95. }
  96. public bool LiteEquals(QualifiedName oCompareObject)
  97. {
  98. return oCompareObject.Assembly == Assembly && oCompareObject.Name == Name;
  99. }
  100. #endregion
  101. #region *** Private Operations ***
  102. private void SetupProperties(AssemblyName? oName)
  103. {
  104. if (oName == null)
  105. throw new ArgumentNullException(nameof(oName));
  106. Assembly = oName.Name;
  107. AssemblyFullName = oName.FullName;
  108. Culture = oName.CultureName;
  109. Token = oName.GetPublicKeyTokenString();
  110. Version = oName.Version!=null ? oName.Version.ToString() : string.Empty;
  111. }
  112. private void ThrowCannotParse(string sReflectionQualifiedName, System.Exception oInnerException)
  113. {
  114. throw new ArgumentException($"Cannot parse QualifiedName '{sReflectionQualifiedName}'!", oInnerException);
  115. }
  116. #endregion
  117. }
  118. }