using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Fantasy.DataStructure.Collection; // ReSharper disable CollectionNeverQueried.Global #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. namespace Fantasy.Assembly { /// /// AssemblyInfo提供有关程序集和类型的信息 /// public sealed class AssemblyInfo { /// /// 唯一标识 /// public readonly long AssemblyIdentity; /// /// 获取或设置与此程序集相关联的 实例。 /// public System.Reflection.Assembly Assembly { get; private set; } /// /// 程序集类型集合,获取一个列表,包含从程序集加载的所有类型。 /// public readonly List AssemblyTypeList = new List(); /// /// 程序集类型分组集合,获取一个分组列表,将接口类型映射到实现这些接口的类型。 /// public readonly OneToManyList AssemblyTypeGroupList = new OneToManyList(); /// /// 初始化 类的新实例。 /// /// public AssemblyInfo(long assemblyIdentity) { AssemblyIdentity = assemblyIdentity; } /// /// 从指定的程序集加载类型信息并进行分类。 /// /// 要加载信息的程序集。 public void Load(System.Reflection.Assembly assembly) { Assembly = assembly; var assemblyTypes = assembly.GetTypes().ToList(); foreach (var type in assemblyTypes) { if (type.IsAbstract || type.IsInterface) { continue; } var interfaces = type.GetInterfaces(); foreach (var interfaceType in interfaces) { AssemblyTypeGroupList.Add(interfaceType, type); } } AssemblyTypeList.AddRange(assemblyTypes); } /// /// 重新加载程序集的类型信息。 /// /// public void ReLoad(System.Reflection.Assembly assembly) { Unload(); Load(assembly); } /// /// 卸载程序集的类型信息。 /// public void Unload() { AssemblyTypeList.Clear(); AssemblyTypeGroupList.Clear(); } } }