Beispiel #1
0
        private static int ComputeSum(ParameterInfo[] parameters, Type[] argTypes)
        {
            Debug.Assert(parameters.Length == argTypes.Length);

            var sum = 0;

            for (var i = 0; i <= parameters.Length - 1; i++)
            {
                sum += ImplicitConverter.GetImplicitConvertScore(argTypes[i], parameters[i].ParameterType);
            }

            return(sum);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        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);
        }
Beispiel #5
0
        private float ComputeScoreForParamArray(ParameterInfo[] parameters, Type[] argTypes)
        {
            var paramArrayParameter = parameters[parameters.Length - 1];
            var fixedParameterCount = paramArrayParameter.Position;
            var fixedParameters     = new ParameterInfo[fixedParameterCount];

            Array.Copy(parameters, fixedParameters, fixedParameterCount);

            var fixedSum = ComputeSum(fixedParameters, FixedArgTypes);
            var paramArrayElementType = paramArrayParameter.ParameterType.GetElementType();
            var paramArraySum         = 0;

            foreach (var argType in ParamArrayArgTypes)
            {
                paramArraySum += ImplicitConverter.GetImplicitConvertScore(argType, paramArrayElementType);
            }

            var score = argTypes.Length > 0 ? (fixedSum + paramArraySum) / argTypes.Length : (float)0;

            // The param array score gets a slight penalty so that it scores worse than direct matches
            return(score + 1);
        }