summaryrefslogtreecommitdiff
path: root/RhSolutions.Parsers/ParsersRegistration.cs
blob: b61e5030bf81a6c894bef21c20eed57d1a3171ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
	}
}