public static void RegisterClassHandler(Type classType, RoutedEvent routedEvent, Delegate handler, bool handledEventsToo)
        {
            List <ClassHandlerNode> rootNodes;

            if (!_classHandlers.TryGetValue(routedEvent, out rootNodes))
            {
                _classHandlers.Add(routedEvent, rootNodes = new List <ClassHandlerNode>());
            }

            RoutedEventHandlerInfo handlerInfo = new RoutedEventHandlerInfo(handler, handledEventsToo);

            foreach (ClassHandlerNode node in rootNodes)
            {
                ClassHandlerNode resultNode = PlaceInNode(node, classType, handlerInfo);
                if (resultNode == null)
                {
                    continue;
                }

                if (resultNode != node)
                {
                    rootNodes.Remove(node);
                    rootNodes.Add(resultNode);
                }

                return;
            }

            ClassHandlerNode newNode = new ClassHandlerNode()
            {
                Class   = classType,
                Handler = handlerInfo,
            };

            rootNodes.Add(newNode);
        }
        private static ClassHandlerNode PlaceInNode(ClassHandlerNode node, Type classType, RoutedEventHandlerInfo handler)
        {
            // If classes are the same, replace existing handler
            Type nodeType = node.Class.GetType();

            if (nodeType == classType)
            {
                node.Handler = handler;
                return(node);
            }

            // If new class is a subclass of the node, insert it
            if (classType.IsSubclassOf(nodeType))
            {
                foreach (ClassHandlerNode subNode in node.SubClassHandlers)
                {
                    ClassHandlerNode resultNode = PlaceInNode(subNode, classType, handler);
                    if (resultNode == null)
                    {
                        continue;
                    }

                    if (resultNode != subNode)
                    {
                        node.SubClassHandlers.Remove(subNode);
                        node.SubClassHandlers.Add(resultNode);
                    }

                    return(node);
                }

                // No interior place found to insert node
                ClassHandlerNode newNode = new ClassHandlerNode()
                {
                    Class   = classType,
                    Handler = handler,
                };

                node.SubClassHandlers.Add(newNode);
                return(node);
            }

            // If the new node is a super of the node, rearrange
            if (nodeType.IsSubclassOf(classType))
            {
                ClassHandlerNode newNode = new ClassHandlerNode()
                {
                    Class   = classType,
                    Handler = handler,
                };

                newNode.SubClassHandlers.Add(node);
                return(newNode);
            }

            return(null);
        }