-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExpressionEngineDIExtensions.cs
More file actions
55 lines (49 loc) · 1.86 KB
/
Copy pathExpressionEngineDIExtensions.cs
File metadata and controls
55 lines (49 loc) · 1.86 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Linq;
using System.Reflection;
using ExpressionEngine.Functions.Base;
using Microsoft.Extensions.DependencyInjection;
namespace ExpressionEngine
{
/// <summary>
/// Extension methods for service collection
/// </summary>
public static class ExpressionEngineDiExtensions
{
/// <summary>
/// Find function implementations registered with <see cref="FunctionRegistrationAttribute"/>.
///
/// The function only scans the given type's assembly
/// </summary>
/// <param name="serviceCollection"></param>
/// <typeparam name="T">Assembly to scan</typeparam>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <returns></returns>
public static IServiceCollection WithFunctionDiscovery<T>(this IServiceCollection serviceCollection)
{
var addFunctionMethodInfo = typeof(ExpressionEngineDiExtensions).GetMethod(nameof(AddFunction));
if (addFunctionMethodInfo == null)
throw new Exception();
var functions =
typeof(T)
.Assembly
.GetTypes()
.Where(t => t.GetCustomAttributes<FunctionRegistrationAttribute>().Any());
foreach (var function in functions)
{
var generic = addFunctionMethodInfo.MakeGenericMethod(function);
generic.Invoke(null, new object[] {serviceCollection});
}
return serviceCollection;
}
/// <summary>
///
/// </summary>
/// <param name="serviceCollection"></param>
/// <typeparam name="T"></typeparam>
private static void AddFunction<T>(IServiceCollection serviceCollection) where T : class, IFunction
{
serviceCollection.AddFunction<T>();
}
}
}