添加项目文件。

This commit is contained in:
chfei committed 2026-08-14 18:00:58 +08:00
1 parent 6a017c9547
commit 58abbb5b4f
86 files changed
+8729

No files matched your search

+197
View File
@@ -0,0 +1,197 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
namespace MAX.Core.Reflections;
public partial class AppTypeFinder : Finder
{
public static AppTypeFinder Instance { get; } = new AppTypeFinder();
/// <summary>
/// 所有程序集
/// </summary>
public static List<Assembly> Assemblies => Instance.GetAssemblies();
public static Exception LoadDLL(string dllPath, out Assembly assembly)
{
return Instance.LoadAssemblyToAppDomain(dllPath, out assembly);
}
/// <summary>
/// 程序集是否匹配
/// </summary>
protected override bool Match(string assemblyName)
{
if (base.Match(assemblyName))
{
if (AvailablePrefix().Exists(assemblyName.StartsWith))
{
return true;
}
return Regex.IsMatch(assemblyName, "^(MAX\\.)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
return false;
}
protected virtual List<string> AvailablePrefix()
{
return ["MAX."];
}
public static IEnumerable<Type> GetAllTypes()
{
List<Type> types = [];
foreach (Assembly item in Assemblies)
{
try
{
types.AddRange(item.GetTypes());
}
catch
{
}
}
return types;
}
/// <summary>
/// 查找当前程序集中所有继承或实现了指定类型的非抽象类。
/// </summary>
/// <param name="targetType">
/// 要匹配的目标类型,可以是:
/// <list type="bullet">
/// <item><description>普通接口类型(例如 <c>IMyService</c>)</description></item>
/// <item><description>开放泛型接口类型(例如 <c>IRepository&lt;&gt;</c>)</description></item>
/// <item><description>普通基类类型(例如 <c>BaseHandler</c>)</description></item>
/// <item><description>开放泛型基类类型(例如 <c>BaseHandler&lt;&gt;</c>)</description></item>
/// </list>
/// </param>
/// <returns>
/// 所有满足条件的具体类(非抽象类)的 <see cref="Type"/> 数组。
/// </returns>
/// <exception cref="ArgumentNullException">
/// 当 <paramref name="targetType"/> 为 <c>null</c> 时抛出。
/// </exception>
/// <remarks>
/// <para>
/// 此方法会遍历 <c>GetAllTypes()</c> 返回的所有类型,并筛选出:
/// <list type="bullet">
/// <item><description>实现了指定接口的类(包括开放泛型接口的封闭实现)</description></item>
/// <item><description>继承了指定基类的类(包括开放泛型基类的封闭实现)</description></item>
/// </list>
/// </para>
/// <para>示例:</para>
/// <code>
/// // 查找所有实现了 IEnumDisplayHandler&lt;T&gt; 的类
/// var handlers = FindAllDerivedTypes(typeof(IEnumDisplayHandler&lt;&gt;));
///
/// // 查找所有实现了 IMyService 的类
/// var services = FindAllDerivedTypes(typeof(IMyService));
///
/// // 查找所有继承 BaseHandler 的类
/// var subclasses = FindAllDerivedTypes(typeof(BaseHandler));
///
/// // 查找所有继承开放泛型基类 BaseHandler&lt;T&gt; 的类
/// var genericSubclasses = FindAllDerivedTypes(typeof(BaseHandler&lt;&gt;));
/// </code>
/// </remarks>
public static Type[] FindAllDerivedTypes(Type targetType)
{
if (targetType == null)
{
throw new ArgumentNullException(nameof(targetType));
}
return GetAllTypes().Where(t =>
{
try
{
if (!t.IsClass || t.IsAbstract)
{
return false;
}
// 开放泛型接口
if (targetType.IsInterface && targetType.IsGenericTypeDefinition)
{
return t.GetInterfaces().Any(i =>
i.IsGenericType && i.GetGenericTypeDefinition() == targetType);
}
// 普通接口
if (targetType.IsInterface)
{
return targetType.IsAssignableFrom(t);
}
// 开放泛型基类
if (targetType.IsGenericTypeDefinition)
{
return IsSubclassOfRawGeneric(t, targetType);
}
// 普通基类
return targetType.IsAssignableFrom(t) && t != targetType;
}
catch
{
return false;
}
})
.ToArray();
}
/// <summary>
/// 检查指定类型是否继承自某个开放泛型基类。
/// </summary>
/// <param name="type">要检查的类型。</param>
/// <param name="generic">开放泛型基类类型,例如 <c>BaseHandler&lt;&gt;</c>。</param>
/// <returns>
/// 如果 <paramref name="type"/> 的继承链中存在指定的开放泛型基类,则返回 <c>true</c>;否则返回 <c>false</c>。
/// </returns>
/// <remarks>
/// 此方法会沿着 <paramref name="type"/> 的继承链向上遍历,
/// 并在每一级检查是否为指定的开放泛型基类定义。
/// </remarks>
private static bool IsSubclassOfRawGeneric(Type type, Type generic)
{
while (type != null && type != typeof(object))
{
Type cur = type.IsGenericType ? type.GetGenericTypeDefinition() : type;
if (generic == cur)
{
return true;
}
type = type.BaseType;
}
return false;
}
/// <summary>
/// 泛型包装方法,查找所有继承或实现了指定类型 <typeparamref name="T"/> 的非抽象类。
/// </summary>
/// <typeparam name="T">
/// 目标类型,可以是接口、开放泛型接口、基类或开放泛型基类。
/// </typeparam>
/// <returns>
/// 所有满足条件的具体类(非抽象类)的 <see cref="Type"/> 数组。
/// </returns>
/// <remarks>
/// 调用示例:
/// <code>
/// var handlers = FindAllDerivedTypes&lt;IEnumDisplayHandler&lt;&gt;&gt;();
/// var services = FindAllDerivedTypes&lt;IMyService&gt;();
/// var subclasses = FindAllDerivedTypes&lt;BaseHandler&gt;();
/// var genericSubclasses = FindAllDerivedTypes&lt;BaseHandler&lt;&gt;&gt;();
/// </code>
/// </remarks>
public static Type[] FindAllDerivedTypes<T>() => FindAllDerivedTypes(typeof(T));
}
+129
View File
@@ -0,0 +1,129 @@
using MAX.Core.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
namespace MAX.Core.Reflections;
public class Finder
{
/// <summary>
/// 跳过的程序集
/// </summary>
private const string SkipAssemblies = "^System|^Microsoft|^mscorlib|^netstandard|^Autofac|^AutoMapper|^EntityFramework|^Newtonsoft|^Castle|^NLog|^Pomelo|^AspectCore|^Xunit|^Nito|^Npgsql|^Exceptionless|^MySqlConnector|^Anonymously|^libuv|^api-ms|^clrcompression|^clretwrc|^clrjit|^coreclr|^dbgshim|^e_sqlite3|^hostfxr|^hostpolicy|^MessagePack|^mscordaccore|^mscordbi|^mscorrc|^sni|^sos|^SOS\\.NETCore|^sos_amd64|^SQLitePCLRaw|^StackExchange|^Swashbuckle|^WindowsBase|^ucrtbase|^DotNetCore\\.CAP|^MongoDB|^Confluent\\.Kafka|^librdkafka|^EasyCaching|^RabbitMQ|^Consul|^Dapper|^EnyimMemcachedCore|^Pipelines|^DnsClient|^IdentityModel|^zlib|^Runtime\\.|^Google|^Grpc|^Internal\\.|^Azure|^DevExpress\\.|^FreeSql\\.|^PresentationFramework|^PresentationCore|^Prism|^DirectWriteForwarder|^MAX\\.Common|^ReflectionHelperTypesPublicWrappers|^UIAutomation|^WindowsFormsIntegration";
/// <summary>
/// 获取程序集列表
/// </summary>
public virtual List<Assembly> GetAssemblies()
{
LoadAssemblies(AppDomain.CurrentDomain.BaseDirectory);
return GetAssembliesFromCurrentAppDomain();
}
/// <summary>
/// 加载程序集到当前应用程序域
/// </summary>
/// <param name="path">目录绝对路径</param>
protected void LoadAssemblies(string path)
{
foreach (string file in Directory.GetFiles(path, "*.dll"))
{
if (Match(Path.GetFileName(file)) == false)
{
continue;
}
LoadAssemblyToAppDomain(file, out _);
}
}
/// <summary>
/// 程序集是否匹配
/// </summary>
protected virtual bool Match(string assemblyName)
{
return Regex.IsMatch(assemblyName, SkipAssemblies, RegexOptions.IgnoreCase | RegexOptions.Compiled) == false;
}
/// <summary>
/// 将程序集添加当前应用程序域
/// </summary>
protected Exception LoadAssemblyToAppDomain(string file, out Assembly assembly)
{
try
{
//Debug.WriteLine($"Load Assembly : {Path.GetFileName(file)}");
AssemblyName assemblyName = AssemblyName.GetAssemblyName(file);
assembly = AppDomain.CurrentDomain.Load(assemblyName);
return null;
}
catch (FileNotFoundException)
{
// 如果找不到文件,回退到 LoadFrom 方式
try
{
assembly = Assembly.LoadFrom(file);
return null;
}
catch (Exception ex)
{
assembly = null;
return ex;
}
}
catch (Exception ex)
{
assembly = null;
return ex;
}
}
/// <summary>
/// 从当前应用程序域获取程序集列表
/// </summary>
private List<Assembly> GetAssembliesFromCurrentAppDomain()
{
List<Assembly> result = [];
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (Match(assembly))
{
result.Add(assembly);
}
}
return result.Distinct().ToList();
}
/// <summary>
/// 程序集是否匹配
/// </summary>
private bool Match(Assembly assembly)
{
return !Regex.IsMatch(assembly.FullName, SkipAssemblies, RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
/// <summary>
/// 查找类型列表
/// </summary>
/// <typeparam name="T">查找类型</typeparam>
/// <param name="assemblies">在指定的程序集列表中查找</param>
public List<Type> Find<T>(List<Assembly> assemblies = null)
{
return Find(typeof(T), assemblies);
}
/// <summary>
/// 查找类型列表
/// </summary>
/// <param name="findType">查找类型</param>
/// <param name="assemblies">在指定的程序集列表中查找</param>
public List<Type> Find(Type findType, List<Assembly> assemblies = null)
{
assemblies ??= GetAssemblies();
return ReflectionHelper.FindTypes(findType, assemblies.ToArray());
}
}