2020-12-28 14:55:48 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Hncore.Infrastructure.Service
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class ServiceIOCExt
|
|
|
|
|
|
{
|
|
|
|
|
|
public static IServiceCollection AutoAddService(this IServiceCollection service, Type fromType = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (fromType == null)
|
|
|
|
|
|
fromType = typeof(IFindService);
|
|
|
|
|
|
|
|
|
|
|
|
var types = Assembly.GetCallingAssembly().GetTypes();
|
|
|
|
|
|
|
|
|
|
|
|
types = types.Where(m => fromType.IsAssignableFrom(m)).ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var type in types)
|
|
|
|
|
|
{
|
|
|
|
|
|
service.AddScoped(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
return service;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|