Пример #1
0
        /// <summary>
        /// Class constructor.
        /// </summary>
        static EntityModelUtils()
        {
            primitiveClrToEdmTypeMap = new Dictionary <Type, PrimitiveDataType>();

            clrToPrimitiveDataTypeConverter = new ClrToPrimitiveDataTypeConverter()
            {
                SpatialResolver = new SpatialClrTypeResolver()
            };
            foreach (var t in EdmDataTypes.GetAllPrimitiveTypes(EdmVersion.Latest))
            {
                Type clrType = clrToPrimitiveDataTypeConverter.ToClrType(t);
                if (clrType == typeof(string) || clrType == typeof(byte[]))
                {
                    // only add the nullable variants to the map for string and byte[]
                    primitiveClrToEdmTypeMap.Add(clrType, t.Nullable());
                }
                else
                {
                    primitiveClrToEdmTypeMap.Add(clrType, t);
                    if (!TestTypeUtils.TypeAllowsNull(clrType))
                    {
                        primitiveClrToEdmTypeMap.Add(typeof(Nullable <>).MakeGenericType(clrType), t.Nullable());
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Returns the primitive type reference for the given Clr type.
        /// </summary>
        /// <param name="clrType">The Clr type to resolve.</param>
        /// <returns>The primitive type reference for the given Clr type.</returns>
        public static IEdmPrimitiveTypeReference GetPrimitiveTypeReference(Type clrType)
        {
            Debug.Assert(clrType != null, "clrType != null");

            Type     targetType = TestTypeUtils.GetNonNullableType(clrType);
            TypeCode typeCode   = Type.GetTypeCode(targetType);
            bool     nullable   = TestTypeUtils.TypeAllowsNull(clrType);

            IEdmPrimitiveType primitiveType = null;

            switch (typeCode)
            {
            case TypeCode.Boolean:
                primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Boolean);
                break;

            case TypeCode.Byte:
                primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Byte);
                break;

            case TypeCode.Decimal:
                primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Decimal);
                break;

            case TypeCode.Double:
                primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Double);
                break;

            case TypeCode.Int16:
                primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int16);
                break;

            case TypeCode.Int32:
                primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int32);
                break;

            case TypeCode.Int64:
                primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int64);
                break;

            case TypeCode.SByte:
                primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.SByte);
                break;

            case TypeCode.String:
                primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.String);
                break;

            case TypeCode.Single:
                primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Single);
                break;

            default:
                if (targetType == typeof(byte[]))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Binary);
                    break;
                }

                if (targetType == typeof(Stream))
                {
                    // stream is always non-nullable
                    nullable      = false;
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Stream);
                    break;
                }

                if (targetType == typeof(DateTimeOffset))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.DateTimeOffset);
                    break;
                }

                if (targetType == typeof(Guid))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Guid);
                    break;
                }

                if (targetType == typeof(TimeSpan))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Duration);
                    break;
                }

                if (typeof(GeographyPoint).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeographyPoint);
                    break;
                }

                if (typeof(GeographyLineString).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeographyLineString);
                    break;
                }

                if (typeof(GeographyPolygon).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeographyPolygon);
                    break;
                }

                if (typeof(GeographyMultiPoint).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeographyMultiPoint);
                    break;
                }

                if (typeof(GeographyMultiLineString).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeographyMultiLineString);
                    break;
                }

                if (typeof(GeographyMultiPolygon).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeographyMultiPolygon);
                    break;
                }

                if (typeof(GeographyCollection).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeographyCollection);
                    break;
                }

                if (typeof(Geography).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Geography);
                    break;
                }

                if (typeof(GeometryPoint).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeometryPoint);
                    break;
                }

                if (typeof(GeometryLineString).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeometryLineString);
                    break;
                }

                if (typeof(GeometryPolygon).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeometryPolygon);
                    break;
                }

                if (typeof(GeometryMultiPoint).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeometryMultiPoint);
                    break;
                }

                if (typeof(GeometryMultiLineString).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeometryMultiLineString);
                    break;
                }

                if (typeof(GeometryMultiPolygon).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeometryMultiPolygon);
                    break;
                }

                if (typeof(GeometryCollection).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.GeometryCollection);
                    break;
                }

                if (typeof(Geometry).IsAssignableFrom(targetType))
                {
                    primitiveType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Geometry);
                    break;
                }

                break;
            }

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

            return(ToTypeReference(primitiveType, nullable));
        }