Example #1
0
 internal static IEdmPrimitiveTypeReference AsPrimitiveOrNull(this IEdmTypeReference typeReference)
 {
     if (typeReference == null)
     {
         return null;
     }
     if (typeReference.TypeKind() != EdmTypeKind.Primitive)
     {
         return null;
     }
     return typeReference.AsPrimitive();
 }
        /// <summary>
        /// Returns the text representation of the current object.
        /// </summary>
        /// <param name="type">Reference to the calling object.</param>
        /// <returns>The text representation of the current object.</returns>
        public static string ToTraceString(this IEdmTypeReference type)
        {
            EdmUtil.CheckArgumentNull(type, "type");
            StringBuilder sb = new StringBuilder();
            sb.Append('[');
            if (type.Definition != null)
            {
                sb.Append(type.Definition.ToTraceString());
                sb.AppendKeyValue(EdmConstants.FacetName_Nullable, type.IsNullable.ToString());
                if (type.IsPrimitive())
                {
                    sb.AppendFacets(type.AsPrimitive());
                }
            }

            sb.Append(']');
            return sb.ToString();
        }
        /// <summary>
        /// Casts an <see cref="IEdmTypeReference"/> to a <see cref="IEdmPrimitiveTypeReference"/> or returns null if this is not supported.
        /// </summary>
        /// <param name="typeReference">The type reference to convert.</param>
        /// <returns>An <see cref="IEdmPrimitiveTypeReference"/> instance or null if the <paramref name="typeReference"/> cannot be converted.</returns>
        internal static IEdmPrimitiveTypeReference AsPrimitiveOrNull(this IEdmTypeReference typeReference)
        {
            DebugUtils.CheckNoExternalCallers();

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

            return typeReference.TypeKind() == EdmTypeKind.Primitive ? typeReference.AsPrimitive() : null;
        }
Example #4
0
        internal static IEdmTypeReference AsActualTypeReference(this IEdmTypeReference type)
        {
            if (type == null || type.TypeKind() != EdmTypeKind.TypeDefinition)
            {
                return type;
            }

            return type.AsPrimitive();
        }
Example #5
0
        /// <summary>
        /// Returns the instance type for the specified <paramref name="typeReference"/> or null if none exists.
        /// </summary>
        /// <param name="typeReference">The type reference to get the instance type for.</param>
        /// <param name="model">The model containing annotations.</param>
        /// <returns>The instance type for the <paramref name="typeReference"/> or null if no instance type exists.</returns>
        /// <remarks>All primitive type references are guaranteed to have an instance type.</remarks>
        public static Type GetInstanceType(this IEdmTypeReference typeReference, IEdmModel model)
        {
            ExceptionUtils.CheckArgumentNotNull(typeReference, "typeReference");
            ExceptionUtils.CheckArgumentNotNull(model, "model");

            if (typeReference.TypeKind() == EdmTypeKind.Primitive)
            {
                IEdmPrimitiveTypeReference primitiveTypeReference = typeReference.AsPrimitive();

                return EdmLibraryExtensions.GetPrimitiveClrType(primitiveTypeReference);
            }

            ODataQueryEdmTypeAnnotation annotation = model.GetAnnotationValue<ODataQueryEdmTypeAnnotation>(typeReference.Definition);
            return annotation == null ? null : annotation.InstanceType;
        }
        /// <summary>
        /// Constructs a new type reference with the specified nullable value
        /// </summary>
        /// <param name="typeReference">The original type reference</param>
        /// <param name="isNullable">The nullable value</param>
        /// <returns>A new type reference, with the specified nullable value</returns>
        public static IEdmTypeReference Nullable(this IEdmTypeReference typeReference, bool isNullable)
        {
            switch (typeReference.TypeKind())
            {
                case EdmTypeKind.Collection:
                    var collection = typeReference.AsCollection();
                    return new EdmCollectionTypeReference(collection.CollectionDefinition());

                case EdmTypeKind.Complex:
                    var complex = typeReference.AsComplex();
                    return new EdmComplexTypeReference(complex.ComplexDefinition(), isNullable);

                case EdmTypeKind.Entity:
                    var entity = typeReference.AsEntity();
                    return new EdmEntityTypeReference(entity.EntityDefinition(), isNullable);

                case EdmTypeKind.EntityReference:
                    var entityRef = typeReference.AsEntityReference();
                    return new EdmEntityReferenceTypeReference(entityRef.EntityReferenceDefinition(), isNullable);

                case EdmTypeKind.Primitive:
                    var primitive = (EdmPrimitiveTypeReference)typeReference.AsPrimitive();
                    return primitive.Nullable(isNullable);

                default:
                    throw new TaupoInvalidOperationException("Unexpected Edm Type Kind: " + typeReference.TypeKind());
            }
        }
        /// <summary>
        /// Casts an <see cref="IEdmTypeReference"/> to a <see cref="IEdmPrimitiveTypeReference"/> or returns null if this is not supported.
        /// </summary>
        /// <param name="typeReference">The type reference to convert.</param>
        /// <returns>An <see cref="IEdmPrimitiveTypeReference"/> instance or null if the <paramref name="typeReference"/> cannot be converted.</returns>
        internal static IEdmPrimitiveTypeReference AsPrimitiveOrNull(this IEdmTypeReference typeReference)
        {
            if (typeReference == null)
            {
                return null;
            }

            return typeReference.TypeKind() == EdmTypeKind.Primitive || typeReference.TypeKind() == EdmTypeKind.TypeDefinition ? typeReference.AsPrimitive() : null;
        }
		public static string ToTraceString(this IEdmTypeReference type)
		{
			EdmUtil.CheckArgumentNull<IEdmTypeReference>(type, "type");
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append('[');
			if (type.Definition != null)
			{
				stringBuilder.Append(type.Definition.ToTraceString());
				bool isNullable = type.IsNullable;
				stringBuilder.AppendKeyValue("Nullable", isNullable.ToString());
				if (type.IsPrimitive())
				{
					stringBuilder.AppendFacets(type.AsPrimitive());
				}
			}
			stringBuilder.Append(']');
			return stringBuilder.ToString();
		}