Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Reflection;
- using System.Linq;
- using Singularity.Core;
- #if NETFX_CORE
- using Windows.UI.Xaml;
- using System.Runtime.InteropServices.WindowsRuntime;
- #else
- using System.Windows;
- using System.Windows.Markup;
- #endif
- /// <summary>
- /// By: Caino MDK
- /// </summary>
- namespace Singularity.Core
- {
- public sealed class DynamicEventHandler : DisposableBase
- {
- private EventInfo eventInfo;
- private Delegate handler;
- #if NETFX_CORE
- private Action<EventRegistrationToken> remove;
- #endif
- public string EventName { get; private set; }
- public object AssociatedObject { get; private set; }
- public Action<object, object> EventHandler { get; private set; }
- public DynamicEventHandler(object associatedObject, string eventName, Action<object, object> eventHandler)
- {
- this.EventName = eventName;
- this.AssociatedObject = associatedObject;
- this.EventHandler = eventHandler;
- this.AttachEvent();
- }
- private void AttachEvent()
- {
- this.eventInfo = this.AssociatedObject.GetType().GetRuntimeEvent(this.EventName);
- if (eventInfo == null)
- return;
- #if NETFX_CORE
- var addMethodInfo = eventInfo.AddMethod;
- var removeMethodInfo = eventInfo.RemoveMethod;
- var eventHandlerType = addMethodInfo.GetParameters()[0].ParameterType;
- this.remove = x => removeMethodInfo.Invoke(this.AssociatedObject, new object[] { x });
- var method = this.GetType().GetRuntimeMethods().First(x => x.Name == "ExecuteCommand");
- this.handler = method.CreateDelegate(eventInfo.EventHandlerType, this);
- WindowsRuntimeMarshal.AddEventHandler(x => this.AddEventHandler(addMethodInfo, x), remove, this.handler);
- #else
- var method = this.GetType().GetRuntimeMethods().First(x => x.Name == "ExecuteCommand");
- this.handler = method.CreateDelegate(eventInfo.EventHandlerType, this);
- this.eventInfo.AddEventHandler(this.AssociatedObject, this.handler);
- #endif
- }
- #if NETFX_CORE
- private EventRegistrationToken AddEventHandler(MethodInfo addMethodInfo, Delegate method)
- {
- var token = addMethodInfo.Invoke(this.AssociatedObject, new object[] { method });
- // in some conditions (I never found out why) the "get" of the event does not return a
- // EventRegistrationToken. In this case we have to use our workaround
- // If someone has an idea why this is happening... please notify me
- if (token == null)
- {
- var eventTokenTable = this.AssociatedObject.GetType().GetTypeInfo().GetDeclaredField("_" + this.EventName);
- // maybe the tokentable is named camelcase
- if (eventTokenTable == null)
- {
- eventTokenTable = this.AssociatedObject.GetType().GetTypeInfo().GetDeclaredField(this.EventName.ToCamelCase());
- }
- // still no table? maybe camelcased with underscore
- if (eventTokenTable == null)
- {
- eventTokenTable = this.AssociatedObject.GetType().GetTypeInfo().GetDeclaredField("_" + this.EventName.ToCamelCase());
- }
- // still no table? fuck this... I don't know
- if (eventTokenTable == null)
- throw new Exception("No token for event found");
- var eventTokenTableInstance = eventTokenTable.GetValue(this.AssociatedObject);
- var getTokenMethod = eventTokenTableInstance.GetType().GetTypeInfo().GetDeclaredMethod("GetToken");
- token = getTokenMethod.Invoke(eventTokenTableInstance, new object[] { method });
- }
- return (EventRegistrationToken)token;
- }
- #endif
- private void DetachEvent()
- {
- #if NETFX_CORE
- if (this.remove == null || this.handler == null)
- return;
- WindowsRuntimeMarshal.RemoveEventHandler(this.remove, this.handler);
- #else
- if (this.eventInfo == null || this.handler == null)
- return;
- this.eventInfo.RemoveEventHandler(this.AssociatedObject, this.handler);
- #endif
- this.eventInfo = null;
- this.handler = null;
- }
- protected override void OnDispose(bool disposeManaged)
- {
- if (disposeManaged)
- {
- this.DetachEvent();
- this.AssociatedObject = null;
- this.EventHandler = null;
- }
- }
- private void ExecuteCommand(object sender, object args)
- {
- if (this.EventHandler != null)
- {
- this.EventHandler(sender, args);
- }
- #if NETFX_CORE
- var handledProperty = args.GetType().GetRuntimeProperty("Handled");
- #else
- var handledProperty = args.GetType().GetProperty("Handled");
- #endif
- if (handledProperty != null)
- {
- handledProperty.SetValue(args, true);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement