Exemple #1
0
            /// <summary>
            /// 查找合适的用户自定义类型转换方法。
            /// </summary>
            /// <returns>合适的用户自定义类型转换方法,如果不存在则为 <c>null</c>。</returns>
            public MethodInfo FindConversion()
            {
                UserConversions convs = GetUserConversions(inputType);

                if (convs != null)
                {
                    Contract.Assume(convs.ConvertToIndex >= 0);
                    for (int i = convs.ConvertToIndex; i < convs.Methods.Length; i++)
                    {
                        UserConversionMethod method = convs.Methods[i];
                        ConversionType       ctype  = ConversionFactory.GetStandardConversion(outputType, method.OutputType);
                        if (ctype == ConversionType.None)
                        {
                            continue;
                        }
                        TypeRelation inputRelation = (method.InputType == inputType) ?
                                                     TypeRelation.Best : TypeRelation.Second;
                        TypeRelation outputRelation = TypeRelation.Best;
                        if (ctype >= ConversionType.ExplicitNumeric)
                        {
                            outputRelation = TypeRelation.Second;
                        }
                        else if (ctype > ConversionType.Identity)
                        {
                            outputRelation = TypeRelation.Thirt;
                        }
                        GetBestConversion(method, inputRelation, outputRelation);
                    }
                }
                convs = GetUserConversions(outputType);
                if (convs != null)
                {
                    for (int i = 0; i < convs.ConvertToIndex; i++)
                    {
                        UserConversionMethod method = convs.Methods[i];
                        ConversionType       ctype  = ConversionFactory.GetStandardConversion(method.InputType, inputType);
                        if (ctype == ConversionType.None)
                        {
                            continue;
                        }
                        TypeRelation outputRelation = (method.OutputType == outputType) ?
                                                      TypeRelation.Best : TypeRelation.Second;
                        TypeRelation inputRelation = TypeRelation.Best;
                        if (ctype >= ConversionType.ExplicitNumeric)
                        {
                            inputRelation = TypeRelation.Second;
                        }
                        else if (ctype > ConversionType.Identity)
                        {
                            inputRelation = TypeRelation.Thirt;
                        }
                        GetBestConversion(method, inputRelation, outputRelation);
                    }
                }
                if (bestMethod.IsUnique)
                {
                    return(bestMethod.Value.Method);
                }
                if (bestMethod.IsAmbig)
                {
                    throw CommonExceptions.AmbiguousUserDefinedConverter(inputType, outputType);
                }
                return(null);
            }