0
0
RhSolutions-Api/RhSolutions.Parsers/ParsersRegistration.cs

25 lines
623 B
C#
Raw Normal View History

2024-01-26 15:50:41 +03:00
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
2024-02-08 17:11:11 +03:00
namespace RhSolutions.Parsers;
2024-01-26 15:50:41 +03:00
2024-02-08 17:11:11 +03:00
public static class ParsersRegistration
2024-01-26 15:50:41 +03:00
{
public static void AddModifiers(this IServiceCollection services)
{
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
2024-02-08 17:11:11 +03:00
.Where(p => p.IsDefined(typeof(ParserKey), true));
2024-01-26 15:50:41 +03:00
foreach (Type t in types)
{
string key = GetModifierKey(t);
2024-02-08 17:11:11 +03:00
services.AddKeyedTransient(typeof(IProductParser), key, t);
2024-01-26 15:50:41 +03:00
}
}
private static string GetModifierKey(Type t)
{
2024-02-08 17:11:11 +03:00
return t.GetCustomAttribute<ParserKey>()?.Value ?? string.Empty;
2024-01-26 15:50:41 +03:00
}
}