Skip to content
New issue

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

建立一个程序集管理缓存,便于快速查找需要的类型 #135

Closed
gmf520 opened this issue Mar 13, 2020 · 0 comments
Closed
Labels
Breaked Changes ⚡ 更新有破坏性,对现有业务实现有较大影响 Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Milestone

Comments

@gmf520
Copy link
Member

gmf520 commented Mar 13, 2020

参考 #120 的建议,对所有类型查找 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();
        }
    }
@gmf520 gmf520 added the Feature 🔨 新功能,新特性 label Mar 13, 2020
@gmf520 gmf520 added this to the v3.1.2 milestone Mar 13, 2020
@gmf520 gmf520 added the Refused 🚫 没有实现价值 label Mar 14, 2020
@gmf520 gmf520 closed this as completed Mar 14, 2020
@gmf520 gmf520 added Breaked Changes ⚡ 更新有破坏性,对现有业务实现有较大影响 and removed Refused 🚫 没有实现价值 labels Mar 17, 2021
@gmf520 gmf520 added the Finished ✔️ 实现并完工 label Mar 17, 2021
@gmf520 gmf520 modified the milestones: v3.1.2, v5.0.4 Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaked Changes ⚡ 更新有破坏性,对现有业务实现有较大影响 Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Projects
None yet
Development

No branches or pull requests

1 participant