summaryrefslogtreecommitdiff
path: root/RhSolutions.MLModifiers/MLModifiersRegistration.cs
blob: 57016ea3d1fb45b49748b93d4885cba8a986fdf1 (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.MLModifiers;

public static class MLModifiersRegistration
{
	public static void AddModifiers(this IServiceCollection services)
	{
		var types = AppDomain.CurrentDomain.GetAssemblies()
			.SelectMany(s => s.GetTypes())
			.Where(p => p.IsDefined(typeof(MLModifierKey), true));

		foreach (Type t in types)
		{
			string key = GetModifierKey(t);
			services.AddKeyedTransient(typeof(IProductMLModifier), key, t);
		}
	}
	
	private static string GetModifierKey(Type t)	
	{		
		return t.GetCustomAttribute<MLModifierKey>()?.Value ?? string.Empty;
	}
}