/// <summary> /// Sets whether reflection over the instance type is allowed or not. /// </summary> /// <param name="type">The type to check.</param> /// <param name="model">Model containing annotations.</param> /// <param name="canReflect">true if reflection over the instance type is allowed; otherwise false.</param> public static void SetCanReflectOnInstanceType(this IEdmType type, IEdmModel model, bool canReflect) { ExceptionUtils.CheckArgumentNotNull(type, "type"); ExceptionUtils.CheckArgumentNotNull(model, "model"); if (type.TypeKind == EdmTypeKind.Primitive) { throw new ODataException(ODataErrorStrings.ODataQueryUtils_CannotSetMetadataAnnotationOnPrimitiveType); } ODataQueryEdmTypeAnnotation annotation = model.GetAnnotationValue <ODataQueryEdmTypeAnnotation>(type); if (annotation == null) { if (canReflect) { annotation = new ODataQueryEdmTypeAnnotation { CanReflectOnInstanceType = true }; model.SetAnnotationValue(type, annotation); } } else { annotation.CanReflectOnInstanceType = canReflect; } }
/// <summary> /// Sets the instance type for the specified <paramref name="type"/>; if null is specified an existing instance type will be removed. /// </summary> /// <param name="type">The type to get the instance type for.</param> /// <param name="model">Model containing annotations.</param> /// <param name="instanceType">The instance type for the <paramref name="type"/> or null to remove an existing instance type.</param> public static void SetInstanceType(this IEdmType type, IEdmModel model, Type instanceType) { ExceptionUtils.CheckArgumentNotNull(type, "type"); ExceptionUtils.CheckArgumentNotNull(model, "model"); if (type.TypeKind == EdmTypeKind.Primitive) { throw new ODataException(ODataErrorStrings.ODataQueryUtils_CannotSetMetadataAnnotationOnPrimitiveType); } ODataQueryEdmTypeAnnotation existingAnnotation = model.GetAnnotationValue <ODataQueryEdmTypeAnnotation>(type); if (existingAnnotation == null) { if (instanceType != null) { ODataQueryEdmTypeAnnotation newAnnotation = new ODataQueryEdmTypeAnnotation { InstanceType = instanceType, }; model.SetAnnotationValue(type, newAnnotation); } } else { existingAnnotation.InstanceType = instanceType; } }
/// <summary> /// Returns the instance type for the specified <paramref name="type"/> or null if none exists. /// </summary> /// <param name="type">The type to get the instance type for.</param> /// <param name="model">The model containing annotations.</param> /// <returns>The instance type for the <paramref name="type"/> or null if no instance type exists.</returns> public static Type GetInstanceType(this IEdmType type, IEdmModel model) { ExceptionUtils.CheckArgumentNotNull(type, "type"); ExceptionUtils.CheckArgumentNotNull(model, "model"); if (type.TypeKind == EdmTypeKind.Primitive) { return(EdmLibraryExtensions.GetPrimitiveClrType((IEdmPrimitiveTypeReference)type.ToTypeReference(false))); } ODataQueryEdmTypeAnnotation annotation = model.GetAnnotationValue <ODataQueryEdmTypeAnnotation>(type); return(annotation == null ? null : annotation.InstanceType); }
public static bool GetCanReflectOnInstanceType(this IEdmTypeReference typeReference, IEdmModel model) { ExceptionUtils.CheckArgumentNotNull(typeReference, "typeReference"); ExceptionUtils.CheckArgumentNotNull(model, "model"); if (typeReference.TypeKind() == EdmTypeKind.Primitive) { // we can reflect over all primitive types return(true); } ODataQueryEdmTypeAnnotation annotation = model.GetAnnotationValue <ODataQueryEdmTypeAnnotation>(typeReference.Definition); return(annotation == null ? false : annotation.CanReflectOnInstanceType); }