예제 #1
0
        private object BuildResourceInstance()
        {
            if (EdmObject == null)
            {
                return(null);
            }

            TypedEdmStructuredObject edmStructruredObject = EdmObject as TypedEdmStructuredObject;

            if (edmStructruredObject != null)
            {
                return(edmStructruredObject.Instance);
            }

            SelectExpandWrapper selectExpandWrapper = EdmObject as SelectExpandWrapper;

            if (selectExpandWrapper != null && selectExpandWrapper.UntypedInstance != null)
            {
                return(selectExpandWrapper.UntypedInstance);
            }

            Type clrType = EdmLibHelpers.GetClrType(StructuredType, EdmModel);

            if (clrType == null)
            {
                throw new InvalidOperationException(Error.Format(SRResources.MappingDoesNotContainResourceType, StructuredType.FullTypeName()));
            }

            object resource = Activator.CreateInstance(clrType);

            foreach (IEdmStructuralProperty property in StructuredType.StructuralProperties())
            {
                object value;
                if (EdmObject.TryGetPropertyValue(property.Name, out value) && value != null)
                {
                    if (value is SelectExpandWrapper)
                    {
                        // Skip the select expand property
                        continue;
                    }

                    string propertyName = EdmLibHelpers.GetClrPropertyName(property, EdmModel);

                    if (TypeHelper.IsCollection(value.GetType()))
                    {
                        DeserializationHelpers.SetCollectionProperty(resource, property, value, propertyName);
                    }
                    else
                    {
                        DeserializationHelpers.SetProperty(resource, propertyName, value);
                    }
                }
            }

            return(resource);
        }
예제 #2
0
        internal static Type GetClrTypeForUntypedDelta(IEdmTypeReference edmType)
        {
            Contract.Assert(edmType != null);

            switch (edmType.TypeKind())
            {
            case EdmTypeKind.Primitive:
                return(EdmLibHelpers.GetClrType(edmType.AsPrimitive(), EdmCoreModel.Instance));

            case EdmTypeKind.Complex:
                return(typeof(EdmComplexObject));

            case EdmTypeKind.Entity:
                return(typeof(EdmEntityObject));

            case EdmTypeKind.Enum:
                return(typeof(EdmEnumObject));

            case EdmTypeKind.Collection:
                IEdmTypeReference elementType = edmType.AsCollection().ElementType();
                if (elementType.IsPrimitive())
                {
                    Type elementClrType = GetClrTypeForUntypedDelta(elementType);
                    return(typeof(List <>).MakeGenericType(elementClrType));
                }
                else if (elementType.IsComplex())
                {
                    return(typeof(EdmComplexObjectCollection));
                }
                else if (elementType.IsEntity())
                {
                    return(typeof(EdmEntityObjectCollection));
                }
                else if (elementType.IsEnum())
                {
                    return(typeof(EdmEnumObjectCollection));
                }
                break;
            }

            throw Error.InvalidOperation(SRResources.UnsupportedEdmType, edmType.ToTraceString(), edmType.TypeKind());
        }
예제 #3
0
        private object BuildEntityInstance()
        {
            if (EdmObject == null)
            {
                return(null);
            }

            TypedEdmEntityObject edmEntityObject = EdmObject as TypedEdmEntityObject;

            if (edmEntityObject != null)
            {
                return(edmEntityObject.Instance);
            }

            Type clrType = EdmLibHelpers.GetClrType(EntityType, EdmModel);

            if (clrType == null)
            {
                throw new InvalidOperationException(Error.Format(SRResources.MappingDoesNotContainEntityType, EntityType.FullName()));
            }

            object resource = Activator.CreateInstance(clrType);

            foreach (IEdmStructuralProperty property in EntityType.StructuralProperties())
            {
                object value;
                if (EdmObject.TryGetPropertyValue(property.Name, out value) && value != null)
                {
                    if (value.GetType().IsCollection())
                    {
                        DeserializationHelpers.SetCollectionProperty(resource, property, value, property.Name);
                    }
                    else
                    {
                        DeserializationHelpers.SetProperty(resource, property.Name, value);
                    }
                }
            }

            return(resource);
        }