ExcelHelper/Utils/HostExtension.cs

32 lines
942 B
C#

using System.Reflection;
using ExcelHelper.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace ExcelHelper.Utils;
internal static class HostExtension
{
public static IHostBuilder AddViewAndViewModel(this IHostBuilder hostBuilder)
{
// Scan all assemblies to find all types that implement IViewModel
var assem = Assembly.GetExecutingAssembly();
var types = assem.GetTypes();
foreach (var type in types)
{
if (type.IsInterface || type.IsAbstract)
{
continue;
}
if (typeof(IViewModel).IsAssignableFrom(type) || typeof(IView).IsAssignableFrom(type))
{
hostBuilder.ConfigureServices((context, services) =>
{
services.AddSingleton(type);
});
}
}
return hostBuilder;
}
}