+ if (!is_sorted) {
+ //
+ // At this point, applicable_type is _one_ of the most derived types
+ // in the set of types containing the methods in this MethodGroup.
+ // Filter the candidates so that they only contain methods from the
+ // most derived types.
+ //
+
+ int finalized = 0; // Number of finalized candidates
+
+ do {
+ // Invariant: applicable_type is a most derived type
+
+ // We'll try to complete Section 14.5.5.1 for 'applicable_type' by
+ // eliminating all it's base types. At the same time, we'll also move
+ // every unrelated type to the end of the array, and pick the next
+ // 'applicable_type'.
+
+ Type next_applicable_type = null;
+ int j = finalized; // where to put the next finalized candidate
+ int k = finalized; // where to put the next undiscarded candidate
+ for (int i = finalized; i < candidate_top; ++i) {
+ Type decl_type = ((MethodBase) candidates[i]).DeclaringType;
+
+ if (decl_type == applicable_type) {
+ candidates[k++] = candidates[j];
+ candidates[j++] = candidates[i];
+ continue;
+ }
+
+ if (IsAncestralType (decl_type, applicable_type))
+ continue;
+
+ if (next_applicable_type != null &&
+ IsAncestralType (decl_type, next_applicable_type))
+ continue;
+
+ candidates[k++] = candidates[i];
+
+ if (next_applicable_type == null ||
+ IsAncestralType (next_applicable_type, decl_type))
+ next_applicable_type = decl_type;
+ }
+
+ applicable_type = next_applicable_type;
+ finalized = j;
+ candidate_top = k;
+ } while (applicable_type != null);
+ }
+
+ //
+ // Now we actually find the best method
+ //
+
+ method = (MethodBase) candidates[0];
+ method_params = candidate_to_form != null && candidate_to_form.Contains (method);
+ for (int ix = 1; ix < candidate_top; ix++){
+ MethodBase candidate = (MethodBase) candidates [ix];
+ bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
+
+ if (BetterFunction (ec, Arguments, arg_count,
+ candidate, cand_params,
+ method, method_params, loc)) {
+ method = candidate;
+ method_params = cand_params;
+ }
+ }
+