FinderBase.cs 1.34 KB
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace NCC.Common.Finder
{
    /// <summary>
    /// 查找器基类
    /// </summary>
    public abstract class FinderBase : IFinderBase
    {
        public FinderBase()
        {
            var path = AppDomain.CurrentDomain.BaseDirectory;
            var files = Directory.GetFiles(path, "*.dll", SearchOption.TopDirectoryOnly)
           .ToArray();
            var assemblies = files.Select(Assembly.LoadFrom).Distinct().ToArray();
            var types = assemblies.SelectMany(x => x.GetTypes()).ToList();
            LoadTypes = types;
        }

        /// <summary>
        /// 获取所有类型
        /// </summary>
        public List<Type> LoadTypes { get; private set; }
        /// <summary>
        /// 所有程序集类型
        /// </summary>
        /// <returns></returns>
        public Type[] FinderAll()
        {
            return Find();
        }
        /// <summary>
        /// 按类型获取
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public Type FinderType(Type type)
        {
            var typeService = Find().FirstOrDefault(x => x == type);
            return typeService;
        }

        public abstract Type[] Find();
    }
}