예제 #1
0
        /// <summary>
        /// Gets the data type of a property value specified in the property instance payload element.
        /// </summary>
        /// <param name="propertyInstance">The property instance payload element to inspect.</param>
        /// <returns>The data type of the property value (can be used to define the metadata for this property).</returns>
        public static IEdmTypeReference GetPayloadEdmElementPropertyValueType(PropertyInstance propertyInstance)
        {
            ExceptionUtilities.CheckArgumentNotNull(propertyInstance, "propertyInstance");

            IEdmTypeReference result = GetEdmTypeFromEntityModelTypeAnnotation(propertyInstance);

            if (result == null)
            {
                switch (propertyInstance.ElementType)
                {
                case ODataPayloadElementType.NullPropertyInstance:
                    NullPropertyInstance nullPropertyInstance = (NullPropertyInstance)propertyInstance;
                    if (nullPropertyInstance.FullTypeName != null)
                    {
                        result = GetPrimitiveEdmType(nullPropertyInstance.FullTypeName);
                        if (result == null)
                        {
                            result = CreateComplexTypeReference(nullPropertyInstance.FullTypeName);
                        }
                    }

                    break;

                case ODataPayloadElementType.PrimitiveProperty:
                    result = GetEdmTypeFromEntityModelTypeAnnotation(((PrimitiveProperty)propertyInstance).Value);
                    if (result == null)
                    {
                        result = GetPrimitiveEdmType(((PrimitiveProperty)propertyInstance).Value.FullTypeName);
                    }

                    break;

                case ODataPayloadElementType.ComplexProperty:
                    result = GetEdmTypeFromEntityModelTypeAnnotation(((ComplexProperty)propertyInstance).Value);
                    if (result == null)
                    {
                        result = CreateComplexTypeReference(((ComplexProperty)propertyInstance).Value.FullTypeName);
                    }

                    break;

                case ODataPayloadElementType.NamedStreamInstance:
                    result = EdmCoreModel.Instance.GetStream(isNullable: false);

                    break;

                case ODataPayloadElementType.PrimitiveMultiValueProperty:
                    PrimitiveMultiValue primitiveMultiValue = ((PrimitiveMultiValueProperty)propertyInstance).Value;
                    result = GetEdmTypeFromEntityModelTypeAnnotation(primitiveMultiValue);
                    if (result == null && primitiveMultiValue.FullTypeName != null)
                    {
                        string itemTypeName = EntityModelUtils.GetCollectionItemTypeName(primitiveMultiValue.FullTypeName);
                        if (itemTypeName != null)
                        {
                            result = EdmCoreModel.GetCollection(GetPrimitiveEdmType(itemTypeName));
                        }
                    }

                    break;

                case ODataPayloadElementType.ComplexMultiValueProperty:
                    ComplexMultiValue complexMultiValue = ((ComplexMultiValueProperty)propertyInstance).Value;
                    result = GetEdmTypeFromEntityModelTypeAnnotation(complexMultiValue);
                    if (result == null && complexMultiValue.FullTypeName != null)
                    {
                        string itemTypeName = EntityModelUtils.GetCollectionItemTypeName(complexMultiValue.FullTypeName);
                        if (itemTypeName != null)
                        {
                            return(EdmCoreModel.GetCollection(CreateComplexTypeReference(itemTypeName)));
                        }
                    }

                    break;

                case ODataPayloadElementType.PrimitiveCollection:
                case ODataPayloadElementType.ComplexInstanceCollection:
                    ExceptionUtilities.Assert(false, "Primitive and complex collections cannot be used in properties but only at the top-level.");
                    return(null);

                default:
                    ExceptionUtilities.Assert(false, "GetPayloadElementPropertyValueType doesn't support '{0}' yet.", propertyInstance.ElementType);
                    return(null);
                }
            }

            // Use the expected type if there's any since it also specifies metadata
            if (result == null)
            {
                ExpectedTypeODataPayloadElementAnnotation expectedTypeAnnotation = propertyInstance.GetAnnotation <ExpectedTypeODataPayloadElementAnnotation>();
                if (expectedTypeAnnotation != null && expectedTypeAnnotation.ExpectedType != null)
                {
                    result = expectedTypeAnnotation.EdmExpectedType;
                }
            }

            return(result);
        }