Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #497

Merged
merged 2 commits into from
Jul 30, 2024
Merged

Dev #497

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/EasilyNET.Core/Misc/AssemblyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace EasilyNET.Core.Misc;
public static class AssemblyHelper
{
private static readonly HashSet<string> Filters = ["dotnet-", "Microsoft.", "mscorlib", "netstandard", "System", "Windows"];
private static readonly IEnumerable<Assembly>? _allAssemblies;
private static readonly IEnumerable<Type>? _allTypes;

/// <summary>
/// 需要排除的项目
Expand All @@ -27,10 +25,20 @@ public static class AssemblyHelper
/// </summary>
static AssemblyHelper()
{
_allAssemblies = DependencyContext.Default?.GetDefaultAssemblyNames().Where(c => c.Name is not null && !Filters.Any(c.Name.StartsWith) && !FilterLibs.Any(c.Name.StartsWith)).Select(Assembly.Load);
_allTypes = _allAssemblies?.SelectMany(c => c.GetTypes());
AllAssemblies = DependencyContext.Default?.GetDefaultAssemblyNames().Where(c => c.Name is not null && !Filters.Any(c.Name.StartsWith) && !FilterLibs.Any(c.Name.StartsWith)).Select(Assembly.Load).ToHashSet();
AllTypes = AllAssemblies?.SelectMany(c => c.GetTypes()).ToHashSet();
}

/// <summary>
/// 获取所有扫描到符合条件的程序集
/// </summary>
public static HashSet<Assembly>? AllAssemblies { get; }

/// <summary>
/// 获取所有扫描到符合条件的程序集中的类型
/// </summary>
public static HashSet<Type>? AllTypes { get; }

/// <summary>
/// 添加排除项目,该排除项目可能会影响AutoDependenceInjection自动注入,请使用的时候自行测试.
/// </summary>
Expand All @@ -42,29 +50,29 @@ static AssemblyHelper()
/// </summary>
/// <param name="assemblyNames"></param>
/// <returns></returns>
public static IEnumerable<Assembly> GetAssembliesByName(params string[] assemblyNames) => assemblyNames.Select(o => AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(AppContext.BaseDirectory, $"{o}.dll")));
public static HashSet<Assembly> GetAssembliesByName(params string[] assemblyNames) => assemblyNames.Select(o => AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(AppContext.BaseDirectory, $"{o}.dll"))).ToHashSet();

/// <summary>
/// 查找指定条件的类型
/// </summary>
public static IEnumerable<Type> FindTypes(Func<Type, bool> predicate) => _allTypes!.Where(predicate).ToArray();
public static HashSet<Type> FindTypes(Func<Type, bool> predicate) => AllTypes!.Where(predicate).ToHashSet();

/// <summary>
/// 查找所有指定特性标记的类型
/// </summary>
/// <typeparam name="TAttribute"></typeparam>
/// <returns></returns>
public static IEnumerable<Type> FindTypesByAttribute<TAttribute>() where TAttribute : Attribute => FindTypesByAttribute(typeof(TAttribute));
public static HashSet<Type> FindTypesByAttribute<TAttribute>() where TAttribute : Attribute => FindTypesByAttribute(typeof(TAttribute));

/// <summary>
/// 查找所有指定特性标记的类型
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static IEnumerable<Type> FindTypesByAttribute(Type type) => _allTypes!.Where(a => a.IsDefined(type, true)).Distinct().ToArray();
public static HashSet<Type> FindTypesByAttribute(Type type) => AllTypes!.Where(a => a.IsDefined(type, true)).Distinct().ToHashSet();

/// <summary>
/// 查找指定条件的类型
/// </summary>
public static IEnumerable<Assembly> FindAllItems(Func<Assembly, bool> predicate) => _allAssemblies!.Where(predicate).ToArray();
public static HashSet<Assembly> FindAllItems(Func<Assembly, bool> predicate) => AllAssemblies!.Where(predicate).ToHashSet();
}