//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ using System; using System.Collections.Generic; namespace GameFramework { public static partial class Utility { /// /// 程序集相关的实用函数。 /// public static class Assembly { private static readonly System.Reflection.Assembly[] s_Assemblies = null; private static readonly Dictionary s_CachedTypes = new Dictionary(StringComparer.Ordinal); static Assembly() { s_Assemblies = AppDomain.CurrentDomain.GetAssemblies(); } /// /// 获取已加载的程序集。 /// /// 已加载的程序集。 public static System.Reflection.Assembly[] GetAssemblies() { return s_Assemblies; } /// /// 获取已加载的程序集中的所有类型。 /// /// 已加载的程序集中的所有类型。 public static Type[] GetTypes() { List results = new List(); foreach (System.Reflection.Assembly assembly in s_Assemblies) { results.AddRange(assembly.GetTypes()); } return results.ToArray(); } /// /// 获取已加载的程序集中的所有类型。 /// /// 已加载的程序集中的所有类型。 public static void GetTypes(List results) { if (results == null) { throw new GameFrameworkException("Results is invalid."); } results.Clear(); foreach (System.Reflection.Assembly assembly in s_Assemblies) { results.AddRange(assembly.GetTypes()); } } /// /// 获取已加载的程序集中的指定类型。 /// /// 要获取的类型名。 /// 已加载的程序集中的指定类型。 public static Type GetType(string typeName) { if (string.IsNullOrEmpty(typeName)) { throw new GameFrameworkException("Type name is invalid."); } Type type = null; if (s_CachedTypes.TryGetValue(typeName, out type)) { return type; } type = Type.GetType(typeName); if (type != null) { s_CachedTypes.Add(typeName, type); return type; } foreach (System.Reflection.Assembly assembly in s_Assemblies) { type = Type.GetType(Text.Format("{0}, {1}", typeName, assembly.FullName)); if (type != null) { s_CachedTypes.Add(typeName, type); return type; } } return null; } } } }