Utility.Assembly.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. namespace GameFramework
  10. {
  11. public static partial class Utility
  12. {
  13. /// <summary>
  14. /// 程序集相关的实用函数。
  15. /// </summary>
  16. public static class Assembly
  17. {
  18. private static readonly System.Reflection.Assembly[] s_Assemblies = null;
  19. private static readonly Dictionary<string, Type> s_CachedTypes = new Dictionary<string, Type>(StringComparer.Ordinal);
  20. static Assembly()
  21. {
  22. s_Assemblies = AppDomain.CurrentDomain.GetAssemblies();
  23. }
  24. /// <summary>
  25. /// 获取已加载的程序集。
  26. /// </summary>
  27. /// <returns>已加载的程序集。</returns>
  28. public static System.Reflection.Assembly[] GetAssemblies()
  29. {
  30. return s_Assemblies;
  31. }
  32. /// <summary>
  33. /// 获取已加载的程序集中的所有类型。
  34. /// </summary>
  35. /// <returns>已加载的程序集中的所有类型。</returns>
  36. public static Type[] GetTypes()
  37. {
  38. List<Type> results = new List<Type>();
  39. foreach (System.Reflection.Assembly assembly in s_Assemblies)
  40. {
  41. results.AddRange(assembly.GetTypes());
  42. }
  43. return results.ToArray();
  44. }
  45. /// <summary>
  46. /// 获取已加载的程序集中的所有类型。
  47. /// </summary>
  48. /// <param name="results">已加载的程序集中的所有类型。</param>
  49. public static void GetTypes(List<Type> results)
  50. {
  51. if (results == null)
  52. {
  53. throw new GameFrameworkException("Results is invalid.");
  54. }
  55. results.Clear();
  56. foreach (System.Reflection.Assembly assembly in s_Assemblies)
  57. {
  58. results.AddRange(assembly.GetTypes());
  59. }
  60. }
  61. /// <summary>
  62. /// 获取已加载的程序集中的指定类型。
  63. /// </summary>
  64. /// <param name="typeName">要获取的类型名。</param>
  65. /// <returns>已加载的程序集中的指定类型。</returns>
  66. public static Type GetType(string typeName)
  67. {
  68. if (string.IsNullOrEmpty(typeName))
  69. {
  70. throw new GameFrameworkException("Type name is invalid.");
  71. }
  72. Type type = null;
  73. if (s_CachedTypes.TryGetValue(typeName, out type))
  74. {
  75. return type;
  76. }
  77. type = Type.GetType(typeName);
  78. if (type != null)
  79. {
  80. s_CachedTypes.Add(typeName, type);
  81. return type;
  82. }
  83. foreach (System.Reflection.Assembly assembly in s_Assemblies)
  84. {
  85. type = Type.GetType(Text.Format("{0}, {1}", typeName, assembly.FullName));
  86. if (type != null)
  87. {
  88. s_CachedTypes.Add(typeName, type);
  89. return type;
  90. }
  91. }
  92. return null;
  93. }
  94. }
  95. }
  96. }