public static DuckValueBindingOption TryBind(Type fromType, Type toType)
        {
            Type coreFromType = Nullable.GetUnderlyingType(fromType);
            Type coreToType   = Nullable.GetUnderlyingType(toType);

            // Cannot convert a nullable value type to a non-nullable value type
            if (coreFromType != null && coreToType == null && toType.IsValueType())
            {
                return(null);
            }

            Type finalFromType = coreFromType ?? fromType;
            Type finalToType   = coreToType ?? toType;

            var method = finalFromType.GetMethod("op_Implicit", new[] { finalFromType });

            if (method == null)
            {
                method = finalToType.GetMethod("op_Implicit", new[] { finalFromType });
                if (method == null)
                {
                    return(null);
                }
            }

            DuckValueBindingOption userConvBinding = new ImplicitUserConversionValueBinding(method);

            return(coreFromType != null ? new ImplicitNullableValueBinding(true, fromType, toType, coreToType, userConvBinding) : userConvBinding);
        }
Example #2
0
 public static DuckValueBindingOption Get(Type fromType, Type toType)
 {
     return(IdentityValueBinding.TryBind(fromType, toType)
            ?? ImplicitNumericValueBinding.TryBind(fromType, toType)
            ?? ImplicitNullableValueBinding.TryBind(fromType, toType)
            ?? ImplicitReferenceValueBinding.TryBind(fromType, toType)
            ?? ImplicitUserConversionValueBinding.TryBind(fromType, toType)
            ?? DuckCastValueBinding.TryBind(fromType, toType)
            ?? NotBindable);
 }
        public static DuckValueBindingOption TryBind(Type fromType, Type toType)
        {
            Type coreFromType = Nullable.GetUnderlyingType(fromType);
            Type coreToType = Nullable.GetUnderlyingType(toType);

            // Cannot convert a nullable value type to a non-nullable value type
            if (coreFromType!=null && coreToType==null && toType.IsValueType)
                return null;

            Type finalFromType = coreFromType ?? fromType;
            Type finalToType = coreToType ?? toType;

            var method = finalFromType.GetMethod("op_Implicit", BindingFlags.Public | BindingFlags.Static, null, new[] {finalFromType}, null);
            if (method==null)
            {
                method = finalToType.GetMethod("op_Implicit", BindingFlags.Public | BindingFlags.Static, null, new[] {finalFromType}, null);
                if (method==null)
                    return null;
            }

            DuckValueBindingOption userConvBinding = new ImplicitUserConversionValueBinding(method);
            return coreFromType!=null ? new ImplicitNullableValueBinding(true, fromType, toType, coreToType, userConvBinding) : userConvBinding;
        }