Blame view

netcore/src/Modularity/Common/NCC.Common/Finder/FinderBase.cs 1.34 KB
de2bd2f9   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  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();
      }
  }