internal bool Dispatch(
            CallbackPolicy policy, object target, object callback,
            bool greedy, IHandler composer, Func <object, bool> results = null)
        {
            if (policy == null)
            {
                throw new ArgumentNullException(nameof(policy));
            }

            PolicyMethods methods = null;

            if (_methods?.TryGetValue(policy, out methods) != true)
            {
                return(false);
            }

            var dispatched = false;
            var indexes    = methods.Keys;
            var keys       = indexes == null ? null
                           : policy.SelectKeys(callback, indexes);

            foreach (var method in methods.GetMethods(keys))
            {
                dispatched = method.Dispatch(target, callback,
                                             composer, results) || dispatched;
                if (dispatched && !greedy)
                {
                    return(true);
                }
            }

            return(dispatched);
        }
        public HandlerDescriptor(Type type)
        {
            foreach (var method in type.GetMethods(Binding))
            {
                MethodDispatch dispatch = null;

                if (method.IsSpecialName || method.IsFamily ||
                    method.DeclaringType == typeof(object))
                {
                    continue;
                }

                var attributes = (DefinitionAttribute[])
                                 Attribute.GetCustomAttributes(method, typeof(DefinitionAttribute), false);

                foreach (var attribute in attributes)
                {
                    var policy = attribute.CallbackPolicy;
                    var rule   = policy.MatchMethod(method, attribute);
                    if (rule == null)
                    {
                        throw new InvalidOperationException(
                                  $"The policy for {attribute.GetType().FullName} rejected method '{method.GetDescription()}'");
                    }

                    dispatch = dispatch ?? new MethodDispatch(method);
                    var binding = rule.Bind(dispatch, attribute);

                    if (_methods == null)
                    {
                        _methods = new Dictionary <CallbackPolicy, PolicyMethods>();
                    }

                    PolicyMethods methods;
                    if (!_methods.TryGetValue(policy, out methods))
                    {
                        methods = new PolicyMethods();
                        _methods.Add(policy, methods);
                    }

                    methods.Insert(binding);
                }
            }
        }