From 645da05c9edcb2002b9735e025763a95ee76f818 Mon Sep 17 00:00:00 2001 From: heguanfeng Date: Wed, 17 Jun 2020 14:04:55 +0800 Subject: [PATCH] Update AssemblyUtility.cs Fixed performance issues caused by too many assemblies --- .../Engine/Engine.Utility/AssemblyUtility.cs | 54 ++++++++++++++----- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/Assets/MotionFramework/Scripts/Runtime/Engine/Engine.Utility/AssemblyUtility.cs b/Assets/MotionFramework/Scripts/Runtime/Engine/Engine.Utility/AssemblyUtility.cs index 16e8cf40..ce9574a1 100644 --- a/Assets/MotionFramework/Scripts/Runtime/Engine/Engine.Utility/AssemblyUtility.cs +++ b/Assets/MotionFramework/Scripts/Runtime/Engine/Engine.Utility/AssemblyUtility.cs @@ -6,35 +6,62 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; using System.Reflection; namespace MotionFramework.Utility { public static class AssemblyUtility { - private static readonly List _cacheTypes = new List(); + public const string MotionFrameworkAssemblyName = "MotionFramework"; + public const string MotionFrameworkAssemblyEditorName = "MotionFramework.Editor"; + public const string UnityDefaultAssemblyName = "Assembly-CSharp"; + public const string UnityDefaultAssemblyEditorName = "Assembly-CSharp-Editor"; + + + private static readonly Dictionary> _cache = new Dictionary>(); static AssemblyUtility() { - _cacheTypes.Clear(); + _cache.Clear(); + } + + /// + /// 获取程序集里的所有类型 + /// + private static List GetTypes(string assemblyName) + { + if (_cache.ContainsKey(assemblyName)) + return _cache[assemblyName]; Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies) { - _cacheTypes.AddRange(assembly.GetTypes()); + if (assembly.GetName().Name == assemblyName) + { + List types = assembly.GetTypes().ToList(); + _cache.Add(assemblyName, types); + return types; + } } + + // 注意:如果没有找到程序集返回空列表 + UnityEngine.Debug.LogWarning($"Not found assembly : {assemblyName}"); + return new List(); } /// /// 获取带继承关系的所有类的类型 /// 父类类型 /// - public static List GetAssignableTypes(System.Type parentType) + public static List GetAssignableTypes(string assemblyName, System.Type parentType) { List result = new List(); - for (int i = 0; i < _cacheTypes.Count; i++) + List cacheTypes = GetTypes(assemblyName); + for (int i = 0; i < cacheTypes.Count; i++) { - Type type = _cacheTypes[i]; + Type type = cacheTypes[i]; // 判断继承关系 if (parentType.IsAssignableFrom(type)) @@ -51,12 +78,13 @@ public static List GetAssignableTypes(System.Type parentType) /// 获取带属性标签的所有类的类型 /// 属性类型 /// - public static List GetAttributeTypes(System.Type attributeType) + public static List GetAttributeTypes(string assemblyName, System.Type attributeType) { List result = new List(); - for (int i = 0; i < _cacheTypes.Count; i++) + List cacheTypes = GetTypes(assemblyName); + for (int i = 0; i < cacheTypes.Count; i++) { - System.Type type = _cacheTypes[i]; + System.Type type = cacheTypes[i]; // 判断属性标签 if (Attribute.IsDefined(type, attributeType)) @@ -72,13 +100,13 @@ public static List GetAttributeTypes(System.Type attributeType) /// /// 父类类型 /// 属性类型 - /// - public static List GetAssignableAttributeTypes(System.Type parentType, System.Type attributeType, bool checkError = true) + public static List GetAssignableAttributeTypes(string assemblyName, System.Type parentType, System.Type attributeType, bool checkError = true) { List result = new List(); - for (int i = 0; i < _cacheTypes.Count; i++) + List cacheTypes = GetTypes(assemblyName); + for (int i = 0; i < cacheTypes.Count; i++) { - Type type = _cacheTypes[i]; + Type type = cacheTypes[i]; // 判断属性标签 if (Attribute.IsDefined(type, attributeType))