QualifiedName.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. var aParts = sReflectionQualifiedName.Split(CC_SEPARATOR)
  28. .Select(x => x.Trim())
  29. .ToList();
  30. if (aParts.Count==0)
  31. return;
  32. if (aParts.Count==1)
  33. {
  34. //jedna se o bud o codebase nebo assembly
  35. bool bCodebase = false;
  36. try
  37. {
  38. var oType = Type.GetType(aParts[0].Trim(), true);
  39. SetupProperties(oType.Assembly.GetName());
  40. bCodebase = true;
  41. }
  42. catch{}
  43. if (bCodebase)
  44. {
  45. //codebase
  46. Name = aParts[0].Trim();
  47. }
  48. else
  49. {
  50. //assembly
  51. try
  52. {
  53. SetupProperties(new AssemblyName(aParts[0].Trim()));
  54. }
  55. catch (System.Exception e)
  56. {
  57. ThrowCannotParse(sReflectionQualifiedName, e);
  58. }
  59. }
  60. }
  61. if (aParts.Count > 1)
  62. {
  63. //jedna se bud o codebase, assembly nebo assembly
  64. try
  65. {
  66. if (aParts[1].IndexOf('=') > 0)
  67. {
  68. //assembly
  69. SetupProperties(new AssemblyName(string.Join(CC_SEPARATOR.ToString() , aParts)));
  70. }
  71. else
  72. {
  73. //codebase, assembly
  74. SetupProperties(new AssemblyName(string.Join(CC_SEPARATOR.ToString(), aParts.Skip(1))));
  75. Name = aParts[0].Trim();
  76. }
  77. }
  78. catch (System.Exception e)
  79. {
  80. ThrowCannotParse(sReflectionQualifiedName, e);
  81. }
  82. }
  83. }
  84. #endregion
  85. #region *** Public Operations ***
  86. public string GetQualifiedNameWithoutVersion()
  87. {
  88. return Name + CC_SEPARATOR + " " + Assembly;
  89. }
  90. public bool LiteEquals(QualifiedName oCompareObject)
  91. {
  92. return oCompareObject.Assembly == Assembly && oCompareObject.Name == Name;
  93. }
  94. #endregion
  95. #region *** Private Operations ***
  96. private void SetupProperties(AssemblyName oName)
  97. {
  98. Assembly = oName.Name;
  99. AssemblyFullName = oName.FullName;
  100. Culture = oName.CultureName;
  101. Token = oName.GetPublicKeyTokenString();
  102. Version = oName.Version!=null ? oName.Version.ToString() : string.Empty;
  103. }
  104. private void ThrowCannotParse(string sReflectionQualifiedName, System.Exception oInnerException)
  105. {
  106. throw new ArgumentException(string.Format("Cannot parse QualifiedName '{0}'!", sReflectionQualifiedName), oInnerException);
  107. }
  108. #endregion
  109. }
  110. }