Exemplo n.º 1
0
        public static void CopyTo(this Dictionary <string, Dictionary <object, Delegate[]> > handlers, Control destination)
        {
            var helper = new CopyEventHandlers();

            foreach (var eventName in handlers.Keys)
            {
                var innerKeyFieldName = helper.KeyEventNameMapping.First(x => x.Value == eventName).Key;

                var info = typeof(Control).GetField(innerKeyFieldName,
                                                    BF.GetField | BF.Static | BF.NonPublic | BF.DeclaredOnly);

                if (info != null)
                {
                    var innerKeyFieldVal = info.GetValue(destination);

                    var targetType             = destination.GetType();
                    var targetEventsInfo       = targetType.GetProperty("Events", BF.Instance | BF.NonPublic);
                    var targetEventHandlerList = (EventHandlerList)targetEventsInfo.GetValue(destination, null);

                    var delegates = handlers[eventName].First().Value;
                    foreach (var @delegate in delegates)
                    {
                        targetEventHandlerList.AddHandler(innerKeyFieldVal, @delegate);
                    }
                }
            }
        }