Esempio n. 1
0
        /// <summary>Gets the type name to be used in the URI for the given <paramref name="type"/>.</summary>
        /// <param name="type">Type to get name for.</param>
        /// <param name="context">Data context used to generate type names for types.</param>
        /// <returns>The name for the <paramref name="type"/>, suitable for including in a URI.</returns>
        internal static string GetTypeNameForUri(Type type, DataServiceContext context)
        {
            Debug.Assert(type != null, "type != null");
            Debug.Assert(context != null, "context != null");
            type = Nullable.GetUnderlyingType(type) ?? type;

            PrimitiveType pt;

            if (PrimitiveType.TryGetPrimitiveType(type, out pt))
            {
                if (pt.HasReverseMapping)
                {
                    return(pt.EdmTypeName);
                }
                else
                {
                    // unsupported primitive type
                    throw new NotSupportedException(Strings.ALinq_CantCastToUnsupportedPrimitive(type.Name));
                }
            }
            else
            {
                return(context.ResolveNameFromType(type) ?? type.FullName);
            }
        }
Esempio n. 2
0
        /// <summary>type edm type string for content</summary>
        /// <param name="propertyType">type to analyze</param>
        /// <returns>edm type string for payload, null for unknown</returns>
        internal static string GetEdmType(Type propertyType)
        {
            PrimitiveType primitiveType;

            if (PrimitiveType.TryGetPrimitiveType(propertyType, out primitiveType))
            {
                if (primitiveType.EdmTypeName != null)
                {
                    return(primitiveType.EdmTypeName);
                }
                else
                {
                    // case StorageType.UInt16:
                    // case StorageType.UInt32:
                    // case StorageType.UInt64:
                    // don't support reverse mappings for these types in this version
                    // allows us to add real server support in the future without a
                    // "breaking change" in the future client
                    throw new NotSupportedException(Strings.ALinq_CantCastToUnsupportedPrimitive(propertyType.Name));
                }
            }
            else
            {
                Debug.Assert(false, "knowntype without reverse mapping");
                return(null);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Returns the primitive property value.
        /// </summary>
        /// <param name="propertyValue">Value of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <returns>Returns the value of the primitive property.</returns>
        private static object ConvertPrimitiveValueToRecognizedODataType(object propertyValue, Type propertyType)
        {
            Debug.Assert(PrimitiveType.IsKnownNullableType(propertyType), "GetPrimitiveValue must be called only for primitive types");
            Debug.Assert(propertyValue == null || PrimitiveType.IsKnownType(propertyValue.GetType()), "GetPrimitiveValue method must be called for primitive values only");

            if (propertyValue == null)
            {
                return(null);
            }

            PrimitiveType primitiveType;

            PrimitiveType.TryGetPrimitiveType(propertyType, out primitiveType);
            Debug.Assert(primitiveType != null, "must be a known primitive type");

            // Do the conversion for types that are not supported by ODataLib e.g. char[], char, etc
            if (propertyType == typeof(Char) ||
                propertyType == typeof(Char[]) ||
                propertyType == typeof(Type) ||
                propertyType == typeof(Uri) ||
                propertyType == typeof(Xml.Linq.XDocument) ||
                propertyType == typeof(Xml.Linq.XElement))
            {
                return(primitiveType.TypeConverter.ToString(propertyValue));
            }
#if !ASTORIA_LIGHT && !PORTABLELIB
            else if (propertyType.FullName == "System.Data.Linq.Binary")
            {
                // For System.Data.Linq.Binary, it is a delay loaded type. Hence checking it based on name.
                // PrimitiveType.IsKnownType checks for binary type based on name and assembly. Hence just
                // checking name here is sufficient, since any other type with the same name, but in different
                // assembly will return false for PrimitiveType.IsKnownNullableType.
                // Since ODataLib does not understand binary type, we need to convert the value to byte[].
                return(((BinaryTypeConverter)primitiveType.TypeConverter).ToArray(propertyValue));
            }
#endif
            else if (primitiveType.EdmTypeName == null)
            {
                // case StorageType.DateTimeOffset:
                // case StorageType.TimeSpan:
                // case StorageType.UInt16:
                // case StorageType.UInt32:
                // case StorageType.UInt64:
                // don't support reverse mappings for these types in this version
                // allows us to add real server support in the future without a
                // "breaking change" in the future client
                throw new NotSupportedException(Strings.ALinq_CantCastToUnsupportedPrimitive(propertyType.Name));
            }

            return(propertyValue);
        }
Esempio n. 4
0
        internal static string GetEdmType(Type propertyType)
        {
            PrimitiveType type;

            if (!PrimitiveType.TryGetPrimitiveType(propertyType, out type))
            {
                return(null);
            }
            if (type.EdmTypeName == null)
            {
                throw new NotSupportedException(Strings.ALinq_CantCastToUnsupportedPrimitive(propertyType.Name));
            }
            return(type.EdmTypeName);
        }
Esempio n. 5
0
        internal static string GetTypeNameForUri(Type type, DataServiceContext context)
        {
            PrimitiveType type2;

            type = Nullable.GetUnderlyingType(type) ?? type;
            if (PrimitiveType.TryGetPrimitiveType(type, out type2))
            {
                if (!type2.HasReverseMapping)
                {
                    throw new NotSupportedException(Strings.ALinq_CantCastToUnsupportedPrimitive(type.Name));
                }
                return(type2.EdmTypeName);
            }
            return(context.ResolveNameFromType(type) ?? type.FullName);
        }
        private string TypeNameForUri(Type type)
        {
            Debug.Assert(type != null, "type != null");
            type = Nullable.GetUnderlyingType(type) ?? type;

            if (ClientConvert.IsKnownType(type))
            {
                if (ClientConvert.IsSupportedPrimitiveTypeForUri(type))
                {
                    return(ClientConvert.ToTypeName(type));
                }

                throw new NotSupportedException(Strings.ALinq_CantCastToUnsupportedPrimitive(type.Name));
            }
            else
            {
                return(this.context.ResolveNameFromType(type) ?? type.FullName);
            }
        }
Esempio n. 7
0
        internal static string GetEdmType(Type propertyType)
        {
            switch ((StorageType)IndexOfStorage(propertyType))
            {
            case StorageType.Boolean:
                return(XmlConstants.EdmBooleanTypeName);

            case StorageType.Byte:
                return(XmlConstants.EdmByteTypeName);

#if !ASTORIA_LIGHT
            case StorageType.Binary:
#endif
            case StorageType.ByteArray:
                return(XmlConstants.EdmBinaryTypeName);

            case StorageType.DateTime:
                return(XmlConstants.EdmDateTimeTypeName);

            case StorageType.Decimal:
                return(XmlConstants.EdmDecimalTypeName);

            case StorageType.Double:
                return(XmlConstants.EdmDoubleTypeName);

            case StorageType.Guid:
                return(XmlConstants.EdmGuidTypeName);

            case StorageType.Int16:
                return(XmlConstants.EdmInt16TypeName);

            case StorageType.Int32:
                return(XmlConstants.EdmInt32TypeName);

            case StorageType.Int64:
                return(XmlConstants.EdmInt64TypeName);

            case StorageType.Single:
                return(XmlConstants.EdmSingleTypeName);

            case StorageType.SByte:
                return(XmlConstants.EdmSByteTypeName);

            case StorageType.DateTimeOffset:
            case StorageType.TimeSpan:
            case StorageType.UInt16:
            case StorageType.UInt32:
            case StorageType.UInt64:
                throw new NotSupportedException(Strings.ALinq_CantCastToUnsupportedPrimitive(propertyType.Name));

            case StorageType.Char:
            case StorageType.CharArray:
            case StorageType.String:
            case StorageType.Type:
            case StorageType.Uri:
            case StorageType.XDocument:
            case StorageType.XElement:
                return(null);

            default:
                Debug.Assert(false, "knowntype without reverse mapping");
                return(null);
            }
        }
Esempio n. 8
0
        /// <summary>type edm type string for content</summary>
        /// <param name="propertyType">type to analyze</param>
        /// <returns>edm type string for payload, null for string and unknown</returns>
        internal static string GetEdmType(Type propertyType)
        {
            switch ((StorageType)IndexOfStorage(propertyType))
            {
            case StorageType.Boolean:
                return(XmlConstants.EdmBooleanTypeName);

            case StorageType.Byte:
                return(XmlConstants.EdmByteTypeName);

#if !ASTORIA_LIGHT // System.Data.Linq not available
            case StorageType.Binary:
#endif
            case StorageType.ByteArray:
                return(XmlConstants.EdmBinaryTypeName);

            case StorageType.DateTime:
                return(XmlConstants.EdmDateTimeTypeName);

            case StorageType.Decimal:
                return(XmlConstants.EdmDecimalTypeName);

            case StorageType.Double:
                return(XmlConstants.EdmDoubleTypeName);

            case StorageType.Guid:
                return(XmlConstants.EdmGuidTypeName);

            case StorageType.Int16:
                return(XmlConstants.EdmInt16TypeName);

            case StorageType.Int32:
                return(XmlConstants.EdmInt32TypeName);

            case StorageType.Int64:
                return(XmlConstants.EdmInt64TypeName);

            case StorageType.Single:
                return(XmlConstants.EdmSingleTypeName);

            case StorageType.SByte:
                return(XmlConstants.EdmSByteTypeName);

            case StorageType.DateTimeOffset:
            case StorageType.TimeSpan:
            case StorageType.UInt16:
            case StorageType.UInt32:
            case StorageType.UInt64:
                // don't support reverse mappings for these types in this version
                // allows us to add real server support in the future without a
                // "breaking change" in the future client
                throw new NotSupportedException(Strings.ALinq_CantCastToUnsupportedPrimitive(propertyType.Name));

            case StorageType.Char:
            case StorageType.CharArray:
            case StorageType.String:
            case StorageType.Type:
            case StorageType.Uri:
            case StorageType.XDocument:
            case StorageType.XElement:
                return(null);    // returning null which implies typeof(string)

            default:
                Debug.Assert(false, "knowntype without reverse mapping");
                return(null);
            }
        }