z8018
3 天以前 d8dc91f9c1fece5711e38edd1b1274cb9e579015
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 
using Microsoft.Extensions.DependencyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEA_Core.Helper
{
    public static class RuntimeExtension
    {
        /// <summary>
        /// 获取当前项目中所有的程序集
        /// </summary>
        /// <returns>返回项目中所有非系统程序集的集合</returns>
        /// <remarks>
        /// 该方法会过滤掉系统程序集和NuGet包,只加载项目自身的程序集。
        /// 加载失败的程序集会被忽略,不会抛出异常。
        /// </remarks>
        public static IList<Assembly> GetAllAssemblies()
        {
            var list = new List<Assembly>();
            var deps = DependencyContext.Default;
            //只加载项目中的程序集
            var libs = deps.CompileLibraries.Where(lib => !lib.Serviceable && lib.Type == "project"); //排除所有的系统程序集、Nuget下载包
            foreach (var lib in libs)
            {
                try
                {
                    var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name));
                    list.Add(assembly);
                }
                catch (Exception e)
                {
                    //Log.Debug(e, "GetAllAssemblies Exception:{ex}", e.Message);
                }
            }
 
            return list;
        }
 
        /// <summary>
        /// 根据程序集名称获取对应的程序集
        /// </summary>
        /// <param name="assemblyName">要查找的程序集名称</param>
        /// <returns>找到的程序集,若未找到则返回null</returns>
        public static Assembly GetAssembly(string assemblyName)
        {
            return GetAllAssemblies().FirstOrDefault(assembly => assembly.FullName.Contains(assemblyName));
        }
 
        /// <summary>
        /// 获取当前应用程序域中所有程序集定义的类型集合
        /// </summary>
        /// <returns>包含所有类型的列表</returns>
        public static IList<Type> GetAllTypes()
        {
            var list = new List<Type>();
            foreach (var assembly in GetAllAssemblies())
            {
                var typeInfos = assembly.DefinedTypes;
                foreach (var typeInfo in typeInfos)
                {
                    list.Add(typeInfo.AsType());
                }
            }
 
            return list;
        }
 
        /// <summary>
        /// 通过程序集名称获取该程序集中的所有类型
        /// </summary>
        /// <param name="assemblyName">要加载的程序集名称</param>
        /// <returns>包含程序集中所有类型的列表</returns>
        public static IList<Type> GetTypesByAssembly(string assemblyName)
        {
            var list = new List<Type>();
            var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));
            var typeInfos = assembly.DefinedTypes;
            foreach (var typeInfo in typeInfos)
            {
                list.Add(typeInfo.AsType());
            }
 
            return list;
        }
 
        /// <summary>
        /// 根据类型名称和基接口类型获取实现类型
        /// </summary>
        /// <param name="typeName">要查找的类型名称</param>
        /// <param name="baseInterfaceType">基接口类型</param>
        /// <returns>符合条件的第一个非抽象、非泛型的类类型,若未找到则返回null</returns>
        public static Type GetImplementType(string typeName, Type baseInterfaceType)
        {
            return GetAllTypes().FirstOrDefault(t =>
            {
                if (t.Name == typeName &&
                    t.GetTypeInfo().GetInterfaces().Any(b => b.Name == baseInterfaceType.Name))
                {
                    var typeInfo = t.GetTypeInfo();
                    return typeInfo.IsClass && !typeInfo.IsAbstract && !typeInfo.IsGenericType;
                }
 
                return false;
            });
        }
    }
}