diff --git a/Src/Newtonsoft.Json/Utilities/MethodBinder.cs b/Src/Newtonsoft.Json/Utilities/MethodBinder.cs index 202321da..ec9d2281 100644 --- a/Src/Newtonsoft.Json/Utilities/MethodBinder.cs +++ b/Src/Newtonsoft.Json/Utilities/MethodBinder.cs @@ -42,7 +42,7 @@ /// Source primitive type /// Target primitive type /// True if source type can be widened to target type, false otherwise - internal static bool CanConvertPrimitive(Type from, Type to) + private static bool CanConvertPrimitive(Type from, Type to) { if (from == to) { @@ -50,9 +50,10 @@ return true; } - int fromMask = 0, toMask = 0; + int fromMask = 0, + toMask = 0; - for (int i = 0; i < PrimitiveTypes.Length && (fromMask == 0 || toMask == 0); i++) + for (int i = 0; i < PrimitiveTypes.Length; i++) { if (PrimitiveTypes[i] == from) { @@ -62,6 +63,11 @@ { toMask = 1 << i; } + + if (fromMask != 0 && toMask != 0) + { + break; + } } return (fromMask & toMask) != 0; @@ -77,14 +83,9 @@ /// True if method can be called with given arguments, false otherwise private static bool FilterParameters(ParameterInfo[] parameters, IList types, bool enableParamArray) { - if (parameters == null) - { - throw new ArgumentNullException(nameof(parameters)); - } - if (types == null) - { - throw new ArgumentNullException(nameof(types)); - } + ValidationUtils.ArgumentNotNull(parameters, nameof(parameters)); + ValidationUtils.ArgumentNotNull(types, nameof(types)); + if (parameters.Length == 0) { // fast check for parameterless methods @@ -161,10 +162,7 @@ public ParametersMatchComparer(IList types, bool enableParamArray) { - if (types == null) - { - throw new ArgumentNullException(nameof(types)); - } + ValidationUtils.ArgumentNotNull(types, nameof(types)); _types = types; _enableParamArray = enableParamArray; @@ -172,14 +170,8 @@ public int Compare(ParameterInfo[] parameters1, ParameterInfo[] parameters2) { - if (parameters1 == null) - { - throw new ArgumentNullException(nameof(parameters1)); - } - if (parameters2 == null) - { - throw new ArgumentNullException(nameof(parameters2)); - } + ValidationUtils.ArgumentNotNull(parameters1, nameof(parameters1)); + ValidationUtils.ArgumentNotNull(parameters2, nameof(parameters2)); // parameterless method wins if (parameters1.Length == 0) @@ -308,14 +300,8 @@ /// Best method overload, or null if none matched public static TMethod SelectMethod(IEnumerable candidates, IList types) where TMethod : MethodBase { - if (candidates == null) - { - throw new ArgumentNullException(nameof(candidates)); - } - if (types == null) - { - throw new ArgumentNullException(nameof(types)); - } + ValidationUtils.ArgumentNotNull(candidates, nameof(candidates)); + ValidationUtils.ArgumentNotNull(types, nameof(types)); // ParamArrays are not supported by ReflectionDelegateFactory // They will be treated like ordinary array arguments