private float ComputeScoreForParamArray(ParameterInfo[] parameters, Type[] argTypes) { ParameterInfo paramArrayParameter = parameters[parameters.Length - 1]; int fixedParameterCount = paramArrayParameter.Position; ParameterInfo[] fixedParameters = new ParameterInfo[fixedParameterCount]; System.Array.Copy(parameters, fixedParameters, fixedParameterCount); int fixedSum = ComputeSum(fixedParameters, MyFixedArgTypes); Type paramArrayElementType = paramArrayParameter.ParameterType.GetElementType(); int paramArraySum = 0; foreach (Type argType in MyParamArrayArgTypes) { paramArraySum += ImplicitConverter.GetImplicitConvertScore(argType, paramArrayElementType); } float score = 0; if (argTypes.Length > 0) { score = (fixedSum + paramArraySum) / argTypes.Length; } else { score = 0; } // The param array score gets a slight penalty so that it scores worse than direct matches return(score + 1); }
/// <summary> /// Compute a score showing how close our method matches the given argument types (for extension methods) /// </summary> /// <param name="parameters"></param> /// <param name="argTypes"></param> /// <returns></returns> private float ComputeScoreExtensionMethodInternal(ParameterInfo[] parameters, Type[] argTypes) { Debug.Assert(parameters.Length == argTypes.Length + 1); int sum = 0; for (int i = 0; i <= argTypes.Length - 1; i++) { sum += ImplicitConverter.GetImplicitConvertScore(argTypes[i], parameters[i + 1].ParameterType); } return(sum); }
private static int ComputeSum(ParameterInfo[] parameters, Type[] argTypes) { Debug.Assert(parameters.Length == argTypes.Length); int sum = 0; for (int i = 0; i <= parameters.Length - 1; i++) { sum += ImplicitConverter.GetImplicitConvertScore(argTypes[i], parameters[i].ParameterType); } return(sum); }
/// <summary> /// Compute a score showing how close our method matches the given argument types (for extension methods) /// </summary> /// <param name="parameters"></param> /// <param name="argTypes"></param> /// <returns></returns> private float ComputeScoreExtensionMethodInternal(ParameterInfo[] parameters, Type[] argTypes) { Debug.Assert(parameters.Length == argTypes.Length + 1); // Our score is the average of the scores of each parameter. The lower the score, the better the match. float sum = 0; for (int i = 0; i < argTypes.Length; i++) { sum += ImplicitConverter.GetImplicitConvertScore(argTypes[i], parameters[i + 1].ParameterType); } return(sum / argTypes.Length); }