private static bool AreValidArgumentsForParameters(Type[] argTypes, ParameterInfo[] parameters) { Debug.Assert(argTypes.Length == parameters.Length); // Match if every given argument is implicitly convertible to the method's corresponding parameter for (var i = 0; i <= argTypes.Length - 1; i++) { if (ImplicitConverter.EmitImplicitConvert(argTypes[i], parameters[i].ParameterType, null) == false) { return(false); } } return(true); }
public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers) { foreach (var methodBase in match) { var parameters = methodBase.GetParameters(); var leftValid = ImplicitConverter.EmitImplicitConvert(leftType, parameters[0].ParameterType, null); var rightValid = ImplicitConverter.EmitImplicitConvert(rightType, parameters[1].ParameterType, null); if (leftValid & rightValid) { return(methodBase); } } return(null); }
private bool IsParamArrayMatch(Type[] argTypes, ParameterInfo[] parameters, ParameterInfo paramArrayParameter) { // Get the count of arguments before the paramArray parameter var fixedParameterCount = paramArrayParameter.Position; var fixedArgTypes = new Type[fixedParameterCount]; var fixedParameters = new ParameterInfo[fixedParameterCount]; // Get the argument types and parameters before the paramArray Array.Copy(argTypes, fixedArgTypes, fixedParameterCount); Array.Copy(parameters, fixedParameters, fixedParameterCount); // If the fixed arguments don't match, we are not a match if (AreValidArgumentsForParameters(fixedArgTypes, fixedParameters) == false) { return(false); } // Get the type of the paramArray ParamArrayElementType = paramArrayParameter.ParameterType.GetElementType(); // Get the types of the arguments passed to the paramArray var paramArrayArgTypes = new Type[argTypes.Length - fixedParameterCount]; Array.Copy(argTypes, fixedParameterCount, paramArrayArgTypes, 0, paramArrayArgTypes.Length); // Check each argument foreach (var argType in paramArrayArgTypes) { if (ImplicitConverter.EmitImplicitConvert(argType, ParamArrayElementType, null) == false) { return(false); } } FixedArgTypes = fixedArgTypes; ParamArrayArgTypes = paramArrayArgTypes; // They all match, so we are a match return(true); }