-
Notifications
You must be signed in to change notification settings - Fork 8
/
ExecutorsDispatcher.cs
86 lines (70 loc) · 2.95 KB
/
ExecutorsDispatcher.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
namespace Winium.StoreApps.Common
{
#region
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
#endregion
/// <summary>
/// Creates instances of command executors based on command name
/// </summary>
public class ExecutorsDispatcher
{
#region Fields
private readonly Dictionary<string, Type> executorsDespatchTable;
#endregion
#region Constructors and Destructors
/// <summary>
/// Mapping between command names and classes that execute corresponding commands.
/// </summary>
/// <param name="assembly">Assemlby where executor classes are stored</param>
/// <param name="baseClassType">Base class for executors</param>
/// <param name="postfix">Class name postfix</param>
public ExecutorsDispatcher(Assembly assembly, Type baseClassType, string postfix = "Executor")
{
this.executorsDespatchTable = new Dictionary<string, Type>();
var executorTypesMap =
assembly.DefinedTypes.Where(type => type.IsSubclassOf(baseClassType))
.ToDictionary(x => x.Name, x => x.AsType());
var fields = typeof(DriverCommand).GetRuntimeFields();
foreach (var localField in fields.Where(field => field.IsStatic && field.FieldType == typeof(string)))
{
Type executorType;
if (executorTypesMap.TryGetValue(localField.Name + postfix, out executorType))
{
this.AddExecutor(localField.GetValue(null).ToString(), executorType);
}
}
}
public void AddExecutor(string commandName, Type executorType)
{
this.executorsDespatchTable.Add(commandName, executorType);
}
#endregion
#region Public Methods and Operators
/// <summary>
/// Creates instance of executor based on command name.
/// If no such command is registred in mapping, then executor of type defaultType is created.
/// If defaultType is null, then null is returned.
/// </summary>
/// <typeparam name="T">Class to cast instantiated executor to</typeparam>
/// <param name="key">Command name, i.e. switchToWindow or findChildElement</param>
/// <param name="defaultType">Type of class that will be instantiated in case if no command executor is found, or null</param>
/// <returns></returns>
public T GetExecutor<T>(string key, Type defaultType = null) where T : class
{
Type executorType;
if (!this.executorsDespatchTable.TryGetValue(key, out executorType))
{
executorType = defaultType;
}
if (executorType == null)
{
return null;
}
return Activator.CreateInstance(executorType) as T;
}
#endregion
}
}