diff --git a/Refit/RequestBuilderImplementation.cs b/Refit/RequestBuilderImplementation.cs index e46ba4f04..20092b544 100644 --- a/Refit/RequestBuilderImplementation.cs +++ b/Refit/RequestBuilderImplementation.cs @@ -132,9 +132,9 @@ RestMethodInfoInternal FindMatchingRestMethodInfo( method => !method.MethodInfo.IsGenericMethod ); - var possibleMethods = possibleMethodsList.ToList(); + var possibleMethods = possibleMethodsList.ToArray(); - if (possibleMethods.Count == 1) + if (possibleMethods.Length == 1) return CloseGenericMethodIfNeeded(possibleMethods[0], genericArgumentTypes); foreach (var method in possibleMethods) @@ -827,9 +827,9 @@ param as IDictionary } //if property, add to populate into HttpRequestMessage.Properties - if (restMethod.PropertyParameterMap.ContainsKey(i)) + if (restMethod.PropertyParameterMap.TryGetValue(i, out var propertyParameter)) { - propertiesToAdd[restMethod.PropertyParameterMap[i]] = param; + propertiesToAdd[propertyParameter] = param; isParameterMappedToRequest = true; } @@ -1208,34 +1208,33 @@ static bool DoNotConvertToQueryMap(object? value) var type = value.GetType(); - bool ShouldReturn() => - type == typeof(string) - || type == typeof(bool) - || type == typeof(char) - || typeof(IFormattable).IsAssignableFrom(type) - || type == typeof(Uri); - // Bail out early & match string - if (ShouldReturn()) + if (ShouldReturn(type)) return true; // Get the element type for enumerables - if (value is IEnumerable enu) - { - var ienu = typeof(IEnumerable<>); - // We don't want to enumerate to get the type, so we'll just look for IEnumerable - var intType = type.GetInterfaces() - .FirstOrDefault( - i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == ienu - ); + if (value is not IEnumerable) + return false; - if (intType != null) - { - type = intType.GetGenericArguments()[0]; - } - } + var ienu = typeof(IEnumerable<>); + // We don't want to enumerate to get the type, so we'll just look for IEnumerable + var intType = type.GetInterfaces() + .FirstOrDefault( + i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == ienu + ); + + if (intType == null) + return false; + + type = intType.GetGenericArguments()[0]; + return ShouldReturn(type); - return ShouldReturn(); + static bool ShouldReturn(Type type) => + type == typeof(string) + || type == typeof(bool) + || type == typeof(char) + || typeof(IFormattable).IsAssignableFrom(type) + || type == typeof(Uri); } static void SetHeader(HttpRequestMessage request, string name, string? value)