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 { 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() { _cache.Clear(); } /// /// 获取程序集 /// public static Assembly GetAssembly(string assemblyName) { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies) { if (assembly.GetName().Name == assemblyName) return assembly; } return null; } /// /// 获取程序集里的所有类型 /// private static List GetTypes(string assemblyName) { if (_cache.ContainsKey(assemblyName)) return _cache[assemblyName]; Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies) { 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(string assemblyName, System.Type parentType) { List result = new List(); List cacheTypes = GetTypes(assemblyName); for (int i = 0; i < cacheTypes.Count; i++) { Type type = cacheTypes[i]; // 判断继承关系 if (parentType.IsAssignableFrom(type)) { if (type.Name == parentType.Name) continue; result.Add(type); } } return result; } /// /// 获取带属性标签的所有类的类型 /// 属性类型 /// public static List GetAttributeTypes(string assemblyName, System.Type attributeType) { List result = new List(); List cacheTypes = GetTypes(assemblyName); for (int i = 0; i < cacheTypes.Count; i++) { System.Type type = cacheTypes[i]; // 判断属性标签 if (Attribute.IsDefined(type, attributeType)) { result.Add(type); } } return result; } /// /// 获取带继承关系和属性标签的所有类的类型 /// /// 父类类型 /// 属性类型 public static List GetAssignableAttributeTypes(string assemblyName, System.Type parentType, System.Type attributeType, bool checkError = true) { List result = new List(); List cacheTypes = GetTypes(assemblyName); for (int i = 0; i < cacheTypes.Count; i++) { Type type = cacheTypes[i]; // 判断属性标签 if (Attribute.IsDefined(type, attributeType)) { // 判断继承关系 if (parentType.IsAssignableFrom(type)) { if (type.Name == parentType.Name) continue; result.Add(type); } else { if(checkError) throw new Exception($"class {type} must inherit from {parentType}."); } } } return result; } } }