移动系统liao
2024-10-15 94da0698c01915b1e340415e080aa03050700d97
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 SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Threading.Tasks;
 
namespace CoreCms.Net.Utility.Helper
{
    /// <summary>
    /// 属性常用方法
    /// </summary>
    public class AssemblyHelper
    {
 
        private static List<Assembly>? _allAssemblies = null;
        /// <summary>
        /// 获取所有程序目录下和常用的程序集 包括一些系统引用程序集
        /// </summary>
        /// <returns> 当前工程下的程序集</returns>
        public static List<Assembly> GetAllAssembly()
        {
 
 
            if (_allAssemblies == null)
            {
                _allAssemblies = new List<Assembly>();
                string? path = null;
                string singlefile = null;
                try
                {
                    path = Assembly.GetEntryAssembly()?.Location;
                }
                catch { }
                if (string.IsNullOrEmpty(path))
                {
                    singlefile = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                    path = Path.GetDirectoryName(singlefile);
                }
                if (string.IsNullOrEmpty(path))
 
                    throw new Exception("获取程序目录出错");
                var dir = new DirectoryInfo(Path.GetDirectoryName(path) ?? "");
 
                var dlls = dir.GetFiles("*.dll", SearchOption.TopDirectoryOnly);
                string[] systemdll = new string[]
                {
                "Microsoft.",
                "System.",
                "Swashbuckle.",
                "ICSharpCode",
                "Newtonsoft.",
                "Oracle.",
                "Pomelo.",
                "SQLitePCLRaw.",
                "Aliyun.OSS",
                "BouncyCastle.",
                "FreeSql.",
                "Google.Protobuf.dll",
                "Humanizer.dll",
                "IdleBus.dll",
                "K4os.",
                "MySql.Data.",
                "Npgsql.",
                "NPOI.",
                "netstandard",
                "MySqlConnector",
                "VueCliMiddleware"
                };
 
                var filtered = dlls.Where(x => systemdll.Any(y => x.Name.StartsWith(y)) == false);
                foreach (var dll in filtered)
                {
                    try
                    {
                        AssemblyLoadContext.Default.LoadFromAssemblyPath(dll.FullName);
                    }
                    catch
                    {
                    }
                }
                var dlllist = AssemblyLoadContext.Default.Assemblies.Where(x => systemdll.Any(y => x.FullName.StartsWith(y)) == false).ToList();
                _allAssemblies.AddRange(dlllist);
            }
            return _allAssemblies;
        }
 
 
 
        /// 获取需要更新或者是创建的 数据库表模型List
        /// </summary>
        /// <typeparam name="T">继承表某个表的所有</typeparam>
        /// <param name="ModeProjectName"></param>
        /// <param name="ModeNamespace"></param>
        /// <param name="ConfigId"></param>
        /// <returns></returns>
        public static Type[] DbCodeFirstModes (string ModeProjectName, string ModeNamespace = null, string ConfigId = null , Type subClass= null, Type thisAttribute=null) 
        {
 
         
#nullable enable
            Type[]? types = GetAllAssembly().Where(x => x.FullName.Contains(ModeProjectName + ",")).FirstOrDefault()?.GetTypes().WhereIF(!string.IsNullOrEmpty(ModeNamespace), name => name.FullName.Contains(ModeNamespace + ".")).ToArray()
                .WhereIF(subClass!=null, x => x.IsSubclassOf(subClass)).ToArray()
                .WhereIF(thisAttribute!=null,x =>( x.GetCustomAttributes(thisAttribute,true).Length>0)).ToArray();
 
            return types;
 
        }
    }
}