-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlConfigEventRegistrar.cs
67 lines (59 loc) · 3.48 KB
/
XmlConfigEventRegistrar.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Reflx;
namespace Emi {
public class XmlConfigEventRegistrar : IEventRegistrar {
readonly String configPath;
readonly IDictionary<String, EventInfo> handlers;
readonly ITypeAndAssemblyParser typeNAsmParser;
public XmlConfigEventRegistrar(ITypeAndAssemblyParser typeNAsmParser) : this(typeNAsmParser, $"{AppDomain.CurrentDomain.BaseDirectory}\\emitter.config.xml") { }
public XmlConfigEventRegistrar(ITypeAndAssemblyParser typeNAsmParser, String configPath) {
this.typeNAsmParser = typeNAsmParser ?? throw new ArgumentNullException(nameof(typeNAsmParser));
this.configPath = configPath ?? throw new ArgumentNullException(nameof(configPath));
handlers = new Dictionary<String, EventInfo>();
}
public void Register(Object classWithHandlers) {
Type classWithHandlersType = classWithHandlers.GetType();
EventInfo[] eventHandlers = classWithHandlersType.GetEvents(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (EventInfo handler in eventHandlers) {
String handlerName = classWithHandlersType.Name + "." + handler.Name;
if (handlers.ContainsKey(handlerName))
handlers[handlerName] = handler;
else
handlers.Add(handlerName, handler);
}
IEnumerable<XmlEventDefinition> events = XmlEventDefinitionLoader.Load(configPath);
foreach (XmlEventDefinition definition in events) {
Type type = typeNAsmParser.GetType(new TypeAndAssembly(definition.Type, definition.Assembly));
Object instance = Activator.CreateInstance(type);
MethodInfo methodInfo = instance
.GetType()
.GetMethods(BindingFlags.Instance | BindingFlags.Public)
.FirstOrDefault(method => method.Name == definition.Method);
if (methodInfo == null)
throw new EmitterException($"Method '{definition.Method}' was not found. Type '{definition.Type}', Assembly '{definition.Assembly}'.");
EventInfo handler = handlers[definition.Name];
var eventDelegate = Delegate.CreateDelegate(handler.EventHandlerType, instance, methodInfo);
MethodInfo addHandler = handler.GetAddMethod(true);
if (definition.OnlyOnce) {
EventHandler<EmitterEventArgs> wrapper = null;
Delegate wrapperDelegate = null;
wrapper = (source, args) => {
Delegate localWrapperDelegate = wrapperDelegate;
Object localEventSource = classWithHandlers;
EventInfo localHandler = handler;
localHandler.GetRemoveMethod(true).Invoke(localEventSource, new[] { localWrapperDelegate });
Delegate localDelegate = eventDelegate;
localDelegate.DynamicInvoke(source, args);
};
wrapperDelegate = Delegate.CreateDelegate(handler.EventHandlerType, wrapper.Target, wrapper.Method);
addHandler.Invoke(classWithHandlers, new Object[] { wrapperDelegate });
}
else
addHandler.Invoke(classWithHandlers, new Object[] { eventDelegate });
}
}
}
}