Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Update AssemblyUtility.cs
Browse files Browse the repository at this point in the history
Fixed performance issues caused by too many assemblies
  • Loading branch information
heguanfeng committed Jun 17, 2020
1 parent 24cd78f commit 645da05
Showing 1 changed file with 41 additions and 13 deletions.
Expand Up @@ -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<Type> _cacheTypes = new List<Type>();
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<string, List<Type>> _cache = new Dictionary<string, List<Type>>();

static AssemblyUtility()
{
_cacheTypes.Clear();
_cache.Clear();
}

/// <summary>
/// 获取程序集里的所有类型
/// </summary>
private static List<Type> 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<Type> types = assembly.GetTypes().ToList();
_cache.Add(assemblyName, types);
return types;
}
}

// 注意:如果没有找到程序集返回空列表
UnityEngine.Debug.LogWarning($"Not found assembly : {assemblyName}");
return new List<Type>();
}

/// <summary>
/// 获取带继承关系的所有类的类型
/// <param name="parentType">父类类型</param>
/// </summary>
public static List<Type> GetAssignableTypes(System.Type parentType)
public static List<Type> GetAssignableTypes(string assemblyName, System.Type parentType)
{
List<Type> result = new List<Type>();
for (int i = 0; i < _cacheTypes.Count; i++)
List<Type> cacheTypes = GetTypes(assemblyName);
for (int i = 0; i < cacheTypes.Count; i++)
{
Type type = _cacheTypes[i];
Type type = cacheTypes[i];

// 判断继承关系
if (parentType.IsAssignableFrom(type))
Expand All @@ -51,12 +78,13 @@ public static List<Type> GetAssignableTypes(System.Type parentType)
/// 获取带属性标签的所有类的类型
/// <param name="attributeType">属性类型</param>
/// </summary>
public static List<Type> GetAttributeTypes(System.Type attributeType)
public static List<Type> GetAttributeTypes(string assemblyName, System.Type attributeType)
{
List<Type> result = new List<Type>();
for (int i = 0; i < _cacheTypes.Count; i++)
List<Type> 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))
Expand All @@ -72,13 +100,13 @@ public static List<Type> GetAttributeTypes(System.Type attributeType)
/// </summary>
/// <param name="parentType">父类类型</param>
/// <param name="attributeType">属性类型</param>
/// <returns></returns>
public static List<Type> GetAssignableAttributeTypes(System.Type parentType, System.Type attributeType, bool checkError = true)
public static List<Type> GetAssignableAttributeTypes(string assemblyName, System.Type parentType, System.Type attributeType, bool checkError = true)
{
List<Type> result = new List<Type>();
for (int i = 0; i < _cacheTypes.Count; i++)
List<Type> cacheTypes = GetTypes(assemblyName);
for (int i = 0; i < cacheTypes.Count; i++)
{
Type type = _cacheTypes[i];
Type type = cacheTypes[i];

// 判断属性标签
if (Attribute.IsDefined(type, attributeType))
Expand Down

0 comments on commit 645da05

Please sign in to comment.