/// <inheritdoc />
        public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            if (modelType == null)
            {
                throw Error.ArgumentNull("modelType");
            }

            if (EdmLibHelpers.GetEdmPrimitiveTypeOrNull(modelType) != null)
            {
                return(new ODataModelBinder());
            }

            if (TypeHelper.IsEnum(modelType))
            {
                return(new ODataModelBinder());
            }

            return(null);
        }
示例#2
0
        internal static object ConvertTo(string valueString, Type type)
        {
            if (valueString == null)
            {
                return(null);
            }

            if (type.IsNullable() && String.Equals(valueString, "null", StringComparison.Ordinal))
            {
                return(null);
            }

            // TODO 1668: ODL beta1's ODataUriUtils.ConvertFromUriLiteral does not support converting uri literal
            // to ODataEnumValue, but beta1's ODataUriUtils.ConvertToUriLiteral supports converting ODataEnumValue
            // to uri literal.
            if (TypeHelper.IsEnum(type))
            {
                string[] values = valueString.Split(new[] { '\'' }, StringSplitOptions.None);
                if (values.Length == 3 && String.IsNullOrEmpty(values[2]))
                {
                    // Remove the type name if the enum value is a fully qualified literal.
                    valueString = values[1];
                }

                Type     enumType     = TypeHelper.GetUnderlyingTypeOrSelf(type);
                object[] parameters   = new[] { valueString, Enum.ToObject(enumType, 0) };
                bool     isSuccessful = (bool)EnumTryParseMethod.MakeGenericMethod(enumType).Invoke(null, parameters);

                if (!isSuccessful)
                {
                    throw Error.InvalidOperation(SRResources.ModelBinderUtil_ValueCannotBeEnum, valueString, type.Name);
                }

                return(parameters[1]);
            }

            // The logic of "public static object ConvertFromUriLiteral(string value, ODataVersion version);" treats
            // the date value string (for example: 2015-01-02) as DateTimeOffset literal, and return a DateTimeOffset
            // object. However, the logic of
            // "object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference);"
            // can return the correct Date object.
            if (type == typeof(Date) || type == typeof(Date?))
            {
                EdmCoreModel model = EdmCoreModel.Instance;
                IEdmPrimitiveTypeReference dateTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(type);
                return(ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4, model, dateTypeReference));
            }

            object value;

            try
            {
                value = ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4);
            }
            catch
            {
                if (type == typeof(string))
                {
                    return(valueString);
                }

                throw;
            }

            bool isNonStandardEdmPrimitive;

            EdmLibHelpers.IsNonstandardEdmPrimitive(type, out isNonStandardEdmPrimitive);

            if (isNonStandardEdmPrimitive)
            {
                return(EdmPrimitiveHelpers.ConvertPrimitiveValue(value, type));
            }
            else
            {
                type = Nullable.GetUnderlyingType(type) ?? type;
                return(System.Convert.ChangeType(value, type, CultureInfo.InvariantCulture));
            }
        }
示例#3
0
 public void IsNullable_RecognizesClassesAndInterfacesAndNullableOfTs(Type type, bool isNullable)
 {
     Assert.Equal(isNullable, EdmLibHelpers.IsNullable(type));
 }
示例#4
0
 public void GetClrType_ReturnsRightClrType(IEdmTypeReference edmTypeReference, Type expectedType)
 {
     Assert.Same(expectedType, EdmLibHelpers.GetClrType(edmTypeReference, GetEdmModel()));
 }