25 lines
624 B
C#
25 lines
624 B
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using System.Reflection;
|
|
|
|
namespace RhSolutions.Parsers;
|
|
|
|
public static class ParsersRegistration
|
|
{
|
|
public static void AddProductParsers(this IServiceCollection services)
|
|
{
|
|
var types = AppDomain.CurrentDomain.GetAssemblies()
|
|
.SelectMany(s => s.GetTypes())
|
|
.Where(p => p.IsDefined(typeof(ParserKey), true));
|
|
|
|
foreach (Type t in types)
|
|
{
|
|
string key = GetParserKey(t);
|
|
services.AddKeyedTransient(typeof(IProductParser), key, t);
|
|
}
|
|
}
|
|
|
|
private static string GetParserKey(Type t)
|
|
{
|
|
return t.GetCustomAttribute<ParserKey>()?.Value ?? string.Empty;
|
|
}
|
|
} |