示例#1
0
        /// <summary>
        /// Returns true if the specified property is a data member that should be serialized
        /// </summary>
        /// <param name="propertyDescriptor">The property to inspect</param>
        /// <returns>true if the specified property is a data member that should be serialized</returns>
        internal static bool IsSerializableDataMember(PropertyDescriptor propertyDescriptor)
        {
            bool serializable = SerializationUtility.IsSerializableDataMember(propertyDescriptor);

            Debug.Assert(!TypeUtils.IsKeyProperty(propertyDescriptor) || serializable, "Key property must be serializable.");
            Debug.Assert(!TypeUtils.IsAssociation(propertyDescriptor) || !serializable, "Association property should not be serializable.");
            Debug.Assert(!TypeUtils.IsComposition(propertyDescriptor) || !serializable, "Composition property should not be serializable.");
            Debug.Assert(!TypeUtils.IsExternalReference(propertyDescriptor) || !serializable, "External Reference property should not be serializable.");
            Debug.Assert(!TypeUtils.IsExcluded(propertyDescriptor) || !serializable, "Excluded property should not be serializable.");

            return(serializable);
        }
        /// <summary>
        /// Loads properties for all the resource types.
        /// </summary>
        private void LoadResourceTypeProperties()
        {
#if DEBUG
            // Remember the types we've visited.
            HashSet <ResourceType> visitedType = new HashSet <ResourceType>(EqualityComparer <ResourceType> .Default);
#endif
            foreach (var resourceType in this.types.Values)
            {
                Type         type = resourceType.InstanceType;
                ResourceType parentResourceType = resourceType.BaseType;
#if DEBUG
                Debug.Assert(parentResourceType == null || visitedType.Contains(parentResourceType), "We must always visit the ancestors of a type before visiting that type.");
                visitedType.Add(resourceType);
#endif
                foreach (PropertyDescriptor pi in TypeDescriptor.GetProperties(type))
                {
                    if (!TypeUtils.IsSerializableDataMember(pi))
                    {
                        continue;
                    }

                    // Only primitive properties are currently supported.
                    ResourceType propertyType = ResourceType.GetPrimitiveResourceType(pi.PropertyType);

                    if (null != propertyType)
                    {
                        ResourceProperty rp = parentResourceType == null ?
                                              null :
                                              this.GetResourceProperty(parentResourceType, DomainDataServiceMetadata.GetNameFromPropertyDescriptor(pi));

                        if (rp == null)
                        {
                            ResourcePropertyKind kind = ResourcePropertyKind.Primitive;
                            if (TypeUtils.IsKeyProperty(pi))
                            {
                                kind |= ResourcePropertyKind.Key;
                            }

                            rp = new ResourceProperty(
                                DomainDataServiceMetadata.GetNameFromPropertyDescriptor(pi),
                                kind,
                                propertyType)
                            {
                                CanReflectOnInstanceTypeProperty = false
                            };

                            // We only need to add rp to the resourceType if rp is not defined on an ancestor type.
                            resourceType.AddProperty(rp);
                        }

                        // We always add rp to this.resourceProperties map even if the property is defined on an ancestor type.
                        this.AddResourcePropertyDescriptor(resourceType, rp, pi);
                    }
                }
            }

#if DEBUG
            foreach (var entry in this.resourceProperties)
            {
                Debug.Assert(entry.Value.Count > 0, "A ResourceType cannot have 0 properties, it should have at least its key property.");
            }
#endif
        }