QualifiedName.cs 4.4 KB

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