123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- //------------------------------------------------------------
- // 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
- {
- /// <summary>
- /// 程序集相关的实用函数。
- /// </summary>
- public static class Assembly
- {
- private static readonly System.Reflection.Assembly[] s_Assemblies = null;
- private static readonly Dictionary<string, Type> s_CachedTypes = new Dictionary<string, Type>(StringComparer.Ordinal);
- static Assembly()
- {
- s_Assemblies = AppDomain.CurrentDomain.GetAssemblies();
- }
- /// <summary>
- /// 获取已加载的程序集。
- /// </summary>
- /// <returns>已加载的程序集。</returns>
- public static System.Reflection.Assembly[] GetAssemblies()
- {
- return s_Assemblies;
- }
- /// <summary>
- /// 获取已加载的程序集中的所有类型。
- /// </summary>
- /// <returns>已加载的程序集中的所有类型。</returns>
- public static Type[] GetTypes()
- {
- List<Type> results = new List<Type>();
- foreach (System.Reflection.Assembly assembly in s_Assemblies)
- {
- results.AddRange(assembly.GetTypes());
- }
- return results.ToArray();
- }
- /// <summary>
- /// 获取已加载的程序集中的所有类型。
- /// </summary>
- /// <param name="results">已加载的程序集中的所有类型。</param>
- public static void GetTypes(List<Type> results)
- {
- if (results == null)
- {
- throw new GameFrameworkException("Results is invalid.");
- }
- results.Clear();
- foreach (System.Reflection.Assembly assembly in s_Assemblies)
- {
- results.AddRange(assembly.GetTypes());
- }
- }
- /// <summary>
- /// 获取已加载的程序集中的指定类型。
- /// </summary>
- /// <param name="typeName">要获取的类型名。</param>
- /// <returns>已加载的程序集中的指定类型。</returns>
- 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;
- }
- }
- }
- }
|