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 (int i = 0; i <= argTypes.Length - 1; i++) { if (ImplicitConverter.EmitImplicitConvert(argTypes[i], parameters[i].ParameterType, null) == false) { return(false); } } return(true); }
public override System.Reflection.MethodBase SelectMethod(System.Reflection.BindingFlags bindingAttr, System.Reflection.MethodBase[] match, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) { foreach (MethodInfo mi in match) { ParameterInfo[] parameters = mi.GetParameters(); bool leftValid = ImplicitConverter.EmitImplicitConvert(_myLeftType, parameters[0].ParameterType, null); bool rightValid = ImplicitConverter.EmitImplicitConvert(_myRightType, parameters[1].ParameterType, null); if (leftValid == true & rightValid == true) { return(mi); } } return(null); }
private bool IsParamArrayMatch(Type[] argTypes, ParameterInfo[] parameters, ParameterInfo paramArrayParameter) { // Get the count of arguments before the paramArray parameter int fixedParameterCount = paramArrayParameter.Position; Type[] fixedArgTypes = new Type[fixedParameterCount]; ParameterInfo[] fixedParameters = new ParameterInfo[fixedParameterCount]; // Get the argument types and parameters before the paramArray System.Array.Copy(argTypes, fixedArgTypes, fixedParameterCount); System.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 Type[] paramArrayArgTypes = new Type[argTypes.Length - fixedParameterCount]; System.Array.Copy(argTypes, fixedParameterCount, paramArrayArgTypes, 0, paramArrayArgTypes.Length); // Check each argument foreach (Type argType in paramArrayArgTypes) { if (ImplicitConverter.EmitImplicitConvert(argType, ParamArrayElementType, null) == false) { return(false); } } MyFixedArgTypes = fixedArgTypes; MyParamArrayArgTypes = paramArrayArgTypes; // They all match, so we are a match return(true); }