ExcelHelper/Utils/HostExtension.cs

32 lines
942 B
C#
Raw Normal View History

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