Beispiel #1
0
        public static int CalculateArgumentScore(Type paramType, Type argType)
        {
            if (null == argType)
            {
                return(!paramType.IsValueType ? ExactMatchScore : -1);
            }

            if (paramType == argType)
            {
                return(ExactMatchScore);
            }
            if (paramType.IsAssignableFrom(argType))
            {
                return(UpCastScore);
            }
            if (argType.IsAssignableFrom(paramType))
            {
                return(DowncastScore);
            }
            if (IsNumericPromotion(paramType, argType))
            {
                return(NumericTypes.IsWideningPromotion(paramType, argType) ? WideningPromotion : NarrowingPromotion);
            }
            var conversion = RuntimeServices.FindImplicitConversionOperator(argType, paramType);

            if (null != conversion)
            {
                return(ImplicitConversionScore);
            }
            return(-1);
        }
Beispiel #2
0
        private int CalculateArgumentScore(Candidate candidate, int argumentIndex, ParameterInfo parameter, Type argType)
        {
            if (parameter.IsOut)
            {
                return(-1);
            }

            if (null == argType)
            {
                if (parameter.ParameterType.IsValueType)
                {
                    return(-1);
                }
                return(ExactMatchScore);
            }
            else
            {
                Type paramType = parameter.ParameterType;

                if (paramType == argType)
                {
                    return(ExactMatchScore);
                }

                if (paramType.IsAssignableFrom(argType))
                {
                    return(UpCastScore);
                }

                if (argType.IsAssignableFrom(paramType))
                {
                    return(DowncastScore);
                }

                if (IsNumericPromotion(paramType, argType))
                {
                    return(PromotionScore);
                }

                MethodInfo conversion = RuntimeServices.FindImplicitConversionOperator(argType, paramType);
                if (null != conversion)
                {
                    candidate.RememberArgumentConversion(argumentIndex, conversion);
                    return(ImplicitConversionScore);
                }
            }
            return(-1);
        }
Beispiel #3
0
        public static int CalculateArgumentScore(Type paramType, Type argType)
        {
            if (null == argType)
            {
                if (paramType.IsValueType)
                {
                    return(-1);
                }
                return(CandidateMethod.ExactMatchScore);
            }
            else
            {
                if (paramType == argType)
                {
                    return(CandidateMethod.ExactMatchScore);
                }

                if (paramType.IsAssignableFrom(argType))
                {
                    return(CandidateMethod.UpCastScore);
                }

                if (argType.IsAssignableFrom(paramType))
                {
                    return(CandidateMethod.DowncastScore);
                }

                if (IsNumericPromotion(paramType, argType))
                {
                    if (NumericTypes.IsWideningPromotion(paramType, argType))
                    {
                        return(WideningPromotion);
                    }
                    return(CandidateMethod.NarrowingPromotion);
                }

                MethodInfo conversion = RuntimeServices.FindImplicitConversionOperator(argType, paramType);
                if (null != conversion)
                {
                    return(CandidateMethod.ImplicitConversionScore);
                }
            }
            return(-1);
        }
Beispiel #4
0
        protected void EmitCoercion(Type actualType, Type expectedType, int score)
        {
            switch (score)
            {
            case CandidateMethod.WideningPromotion:
            case CandidateMethod.NarrowingPromotion:
                EmitPromotion(expectedType, actualType);
                break;

            case CandidateMethod.ImplicitConversionScore:
                EmitCastOrUnbox(actualType);
                _il.Emit(OpCodes.Call, RuntimeServices.FindImplicitConversionOperator(actualType, expectedType));
                break;

            default:
                EmitCastOrUnbox(expectedType);
                break;
            }
        }