We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
参考 #120 的建议,对所有类型查找 ITypeFinder相关功能进行改进 建立一个程序集管理类OSharp.Reflection.AssemblyManager,并提供一些类型查找功能,便于快速查找符合某些条件的类型
ITypeFinder
OSharp.Reflection.AssemblyManager
/// <summary> /// 程序集管理器 /// </summary> public static class AssemblyManager { private static readonly string[] Filters = { "dotnet-", "Microsoft.", "mscorlib", "netstandard", "System", "Windows" }; private static Assembly[] _allAssemblies; private static Type[] _allExportTypes; static AssemblyManager() { AssemblyFilterFunc = name => { return name.Name != null && !Filters.Any(m => name.Name.StartsWith(m)); }; } /// <summary> /// 设置 程序集过滤器 /// </summary> public static Func<AssemblyName, bool> AssemblyFilterFunc { private get; set; } /// <summary> /// 获取 所有程序集 /// </summary> public static Assembly[] AllAssemblies { get { if (_allAssemblies == null) { Init(); } return _allAssemblies; } } /// <summary> /// 获取 所有公开类型 /// </summary> public static Type[] AllExportTypes { get { if (_allExportTypes == null) { Init(); } return _allExportTypes; } } /// <summary> /// 初始化 /// </summary> public static void Init() { if (AssemblyFilterFunc == null) { throw new OsharpException("AssemblyManager.AssemblyFilterFunc 不能为空"); } _allAssemblies = DependencyContext.Default.GetDefaultAssemblyNames() .Where(AssemblyFilterFunc).Select(Assembly.Load).ToArray(); _allExportTypes = _allAssemblies.SelectMany(m => m.ExportedTypes).ToArray(); } /// <summary> /// 查找指定条件的类型 /// </summary> public static Type[] FindTypes(Func<Type, bool> predicate) { return AllExportTypes.Where(predicate).ToArray(); } /// <summary> /// 查找指定基类的实现类型 /// </summary> public static Type[] FindTypesByBase<TBaseType>() { Type baseType = typeof(TBaseType); return FindTypesByBase(baseType); } /// <summary> /// 查找指定基类的实现类型 /// </summary> public static Type[] FindTypesByBase(Type baseType) { return AllExportTypes.Where(type => type.IsDeriveClassFrom(baseType)).Distinct().ToArray(); } /// <summary> /// 查找指定Attribute特性的实现类型 /// </summary> public static Type[] FindTypesByAttribute<TAttribute>(bool inherit = true) { Type attributeType = typeof(TAttribute); return FindTypesByAttribute(attributeType, inherit); } /// <summary> /// 查找指定Attribute特性的实现类型 /// </summary> public static Type[] FindTypesByAttribute(Type attributeType, bool inherit = true) { return AllExportTypes.Where(type => type.IsDefined(attributeType, inherit)).Distinct().ToArray(); } }
The text was updated successfully, but these errors were encountered:
feat(core): 建立一个程序集管理缓存,便于快速查找需要的类型 #135
7446343
No branches or pull requests
参考 #120 的建议,对所有类型查找
ITypeFinder
相关功能进行改进建立一个程序集管理类
OSharp.Reflection.AssemblyManager
,并提供一些类型查找功能,便于快速查找符合某些条件的类型The text was updated successfully, but these errors were encountered: