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

feat: 优化函数返回类型 #537

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ public virtual void Dispose()
private static IEnumerable<IAppModule> GetAllEnabledModule(IServiceCollection services)
{
var types = AssemblyHelper.FindTypes(AppModule.IsAppModule);
var modules = types.Select(o => CreateModule(services, o)).ToList().Where(c => c is not null);
return modules!;
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
foreach (var o in types)
{
var c = CreateModule(services, o);
if (c is not null) yield return c;
}
}

/// <summary>
Expand Down
18 changes: 9 additions & 9 deletions src/EasilyNET.Core/Misc/AssemblyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ 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).ToHashSet();
AllTypes = AllAssemblies?.SelectMany(c => c.GetTypes()).ToHashSet();
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.AsNotNull().SelectMany(c => c.GetTypes());
}

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

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

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

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

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

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

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