-
Notifications
You must be signed in to change notification settings - Fork 796
/
DataScriptRegistration.cs
47 lines (40 loc) · 1.55 KB
/
DataScriptRegistration.cs
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
using Microsoft.Extensions.DependencyInjection;
namespace Serenity.Web;
/// <summary>
/// Contains helper methods for data script registration
/// </summary>
public class DataScriptRegistration
{
/// <summary>
/// Creates and registers dynamic scripts for types with <see cref="DataScriptAttribute"/>
/// </summary>
/// <param name="scriptManager">Dynamic script manager</param>
/// <param name="typeSource">Type source</param>
/// <param name="serviceProvider">Service provider</param>
/// <exception cref="ArgumentNullException">Script manager, type source or ,
/// service provider is null</exception>
public static void RegisterDataScripts(IDynamicScriptManager scriptManager,
ITypeSource typeSource, IServiceProvider serviceProvider)
{
ArgumentNullException.ThrowIfNull(scriptManager);
ArgumentNullException.ThrowIfNull(typeSource);
ArgumentNullException.ThrowIfNull(serviceProvider);
DataScriptAttribute attr;
foreach (var type in typeSource.GetTypes())
{
if (type.IsAbstract ||
type.IsInterface ||
type.IsGenericTypeDefinition ||
!type.IsPublic)
continue;
attr = type.GetCustomAttribute<DataScriptAttribute>();
if (attr != null)
{
var script = (INamedDynamicScript)ActivatorUtilities
.CreateInstance(serviceProvider, type);
scriptManager.Register(script);
continue;
}
}
}
}