public AutoInjectAttribute(InjectLifeTime injectLifeTime) { LifeTime = injectLifeTime; }
/// <summary> /// 注册单例 该方法不使用特性 程序集内包含指定名称的类进行自动注入 /// </summary> /// <param name="services"></param> /// <param name="currentAssembly"></param> /// <returns></returns> public static IServiceCollection AddDependency(this IServiceCollection services, Assembly currentAssembly, InjectLifeTime injectLifeTime) { List <Type> types = currentAssembly.GetTypes().Where(o => !o.IsInterface && !o.IsGenericType).ToList(); if (types.IsEmpty()) { return(services); } foreach (var currentType in types) { List <Type> interfaces = currentType.GetInterfaces().ToList(); if (interfaces.IsEmpty()) { continue; } foreach (var currentInterface in interfaces) { if (injectLifeTime == InjectLifeTime.Singleton) { services.AddSingleton(currentInterface, currentType); } if (injectLifeTime == InjectLifeTime.Scoped) { services.AddScoped(currentInterface, currentType); } if (injectLifeTime == InjectLifeTime.Transient) { services.AddTransient(currentInterface, currentType); } } } return(services); }