/// <summary> /// 更新最优的输入/输出类型。 /// </summary> /// <param name="methodType">类型转换方法的输入/输出类型。</param> /// <param name="relation">实际输入类型与方法输入/输出类型间的关系。</param> /// <param name="relationJudge">方法输入/输出类型和最优类型间的关系判定。</param> /// <param name="bestType">当前的最优类型。</param> /// <param name="bestRelation">当前最优类型与实际输入/输出类型间的关系。</param> private void UpdateBestType(Type methodType, TypeRelation relation, bool relationJudge, ref Type bestType, ref TypeRelation bestRelation) { // 当前选择更差。 if (relation > bestRelation) { return; } // 当前选择更优,或者当前选择与最佳选择相同,但最佳选择没有类型填充。 if (relation < bestRelation || bestType == null) { bestType = methodType; bestRelation = relation; bestMethod.Reset(); return; } ConversionType ctype = ConversionFactory.GetStandardConversion(methodType, bestType); if (ctype == ConversionType.None) { // 找不到类型转换关系,令最佳选择前进至下一级别,并清除类型填充。 bestType = null; bestRelation--; bestMethod.Reset(); return; } if (ctype.IsImplicit() == relationJudge) { // 当前选择被最优选择包含。 if (bestRelation == TypeRelation.Second) { bestType = methodType; bestMethod.Reset(); } } else if (bestRelation == TypeRelation.Thirt) { // 当前选择包含最优选择。 bestType = methodType; bestMethod.Reset(); } }
/// <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); }