平时在用依赖注入的时候由于普遍都需要批量扫描但原生不支持,我们就都用autofac但这需要去替换容器,.netcore3.X后对替换容器又有了新写法,这里就自己实现对原生容器的批量扫描就不用去使用autofac了
直接在nuget搜索:DIBatchRegister即可 使用用例: services.Register(new List() {"ConsoleApp2"});//程序集名称,必须要有默认空参构造
/// <summary>
/// 必须要有无参构造
/// </summary>
/// <param name="services"></param>
/// <param name="pathList">程序集名称</param>
/// <returns></returns>
public static IServiceCollection Register(this IServiceCollection services, List<string> pathList)
{
//使用原生di实现批量注册
if (pathList == null || pathList.Count <= 0) return services;
List<Type> types = new List<Type>();
foreach (string s in pathList)
{
Assembly assembly = Assembly.Load(s);
types.AddRange(assembly