public void IsNonstandardEdmPrimitive_Returns_False(Type primitiveType) { bool isNonstandardEdmPrimtive; Type resultMappedType = EdmLibHelpers.IsNonstandardEdmPrimitive(primitiveType, out isNonstandardEdmPrimtive); Assert.False(isNonstandardEdmPrimtive); Assert.Equal(primitiveType, resultMappedType); }
internal static object ConvertTo(string valueString, Type type) { if (valueString == null) { 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]; } if (type.IsNullable() && String.Equals(valueString, "null", StringComparison.Ordinal)) { return(null); } 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]); } object value = ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4); bool isNonStandardEdmPrimitive; EdmLibHelpers.IsNonstandardEdmPrimitive(type, out isNonStandardEdmPrimitive); if (isNonStandardEdmPrimitive) { return(EdmPrimitiveHelpers.ConvertPrimitiveValue(value, type)); } else { type = Nullable.GetUnderlyingType(type) ?? type; return(Convert.ChangeType(value, type, CultureInfo.InvariantCulture)); } }
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)); } }