|
| 1 | +// Copyright (c) Arjen Post and contributors. See LICENSE and NOTICE in the project root for license information. |
| 2 | + |
| 3 | +#if !ASPNETCORE2 |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | + |
| 8 | +namespace PartialResponse.AspNetCore.Mvc.Formatters.Json.Internal |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Helper related to generic interface definitions and implementing classes. |
| 12 | + /// </summary> |
| 13 | + internal static class ClosedGenericMatcher |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Determine whether <paramref name="queryType"/> is or implements a closed generic <see cref="Type"/> |
| 17 | + /// created from <paramref name="interfaceType"/>. |
| 18 | + /// </summary> |
| 19 | + /// <param name="queryType">The <see cref="Type"/> of interest.</param> |
| 20 | + /// <param name="interfaceType">The open generic <see cref="Type"/> to match. Usually an interface.</param> |
| 21 | + /// <returns> |
| 22 | + /// The closed generic <see cref="Type"/> created from <paramref name="interfaceType"/> that |
| 23 | + /// <paramref name="queryType"/> is or implements. <c>null</c> if the two <see cref="Type"/>s have no such |
| 24 | + /// relationship. |
| 25 | + /// </returns> |
| 26 | + /// <remarks> |
| 27 | + /// This method will return <paramref name="queryType"/> if <paramref name="interfaceType"/> is |
| 28 | + /// <c>typeof(KeyValuePair{,})</c>, and <paramref name="queryType"/> is |
| 29 | + /// <c>typeof(KeyValuePair{string, object})</c>. |
| 30 | + /// </remarks> |
| 31 | + public static Type ExtractGenericInterface(Type queryType, Type interfaceType) |
| 32 | + { |
| 33 | + if (queryType == null) |
| 34 | + { |
| 35 | + throw new ArgumentNullException(nameof(queryType)); |
| 36 | + } |
| 37 | + |
| 38 | + if (interfaceType == null) |
| 39 | + { |
| 40 | + throw new ArgumentNullException(nameof(interfaceType)); |
| 41 | + } |
| 42 | + |
| 43 | + if (IsGenericInstantiation(queryType, interfaceType)) |
| 44 | + { |
| 45 | + // queryType matches (i.e. is a closed generic type created from) the open generic type. |
| 46 | + return queryType; |
| 47 | + } |
| 48 | + |
| 49 | + // Otherwise check all interfaces the type implements for a match. |
| 50 | + // - If multiple different generic instantiations exists, we want the most derived one. |
| 51 | + // - If that doesn't break the tie, then we sort alphabetically so that it's deterministic. |
| 52 | + // |
| 53 | + // We do this by looking at interfaces on the type, and recursing to the base type |
| 54 | + // if we don't find any matches. |
| 55 | + return GetGenericInstantiation(queryType, interfaceType); |
| 56 | + } |
| 57 | + |
| 58 | + private static bool IsGenericInstantiation(Type candidate, Type interfaceType) |
| 59 | + { |
| 60 | + return |
| 61 | + candidate.GetTypeInfo().IsGenericType && |
| 62 | + candidate.GetGenericTypeDefinition() == interfaceType; |
| 63 | + } |
| 64 | + |
| 65 | + private static Type GetGenericInstantiation(Type queryType, Type interfaceType) |
| 66 | + { |
| 67 | + Type bestMatch = null; |
| 68 | + var interfaces = queryType.GetInterfaces(); |
| 69 | + foreach (var @interface in interfaces) |
| 70 | + { |
| 71 | + if (IsGenericInstantiation(@interface, interfaceType)) |
| 72 | + { |
| 73 | + if (bestMatch == null) |
| 74 | + { |
| 75 | + bestMatch = @interface; |
| 76 | + } |
| 77 | + else if (StringComparer.Ordinal.Compare(@interface.FullName, bestMatch.FullName) < 0) |
| 78 | + { |
| 79 | + bestMatch = @interface; |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + // There are two matches at this level of the class hierarchy, but @interface is after |
| 84 | + // bestMatch in the sort order. |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (bestMatch != null) |
| 90 | + { |
| 91 | + return bestMatch; |
| 92 | + } |
| 93 | + |
| 94 | + // BaseType will be null for object and interfaces, which means we've reached 'bottom'. |
| 95 | + var baseType = queryType?.GetTypeInfo().BaseType; |
| 96 | + if (baseType == null) |
| 97 | + { |
| 98 | + return null; |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + return GetGenericInstantiation(baseType, interfaceType); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | +#endif |
0 commit comments