/// <summary>
        /// Returns the resource type for the corresponding clr type.
        /// If the given clr type is a collection, then resource type describes the element type of the collection.
        /// </summary>
        /// <param name="type">clrType whose corresponding resource type needs to be returned</param>
        /// <returns>Returns the resource type</returns>
        private ResourceType GetNonPrimitiveType(Type type)
        {
            Debug.Assert(type != null, "type != null");

            // Check for the type directly first
            ResourceType resourceType = this.ResolveNonPrimitiveType(type);

            if (resourceType == null)
            {
                // check for ienumerable types
                Type elementType = BaseServiceProvider.GetIEnumerableElement(type);
                if (elementType != null)
                {
                    resourceType = ResourceType.GetPrimitiveResourceType(elementType);
                    if (resourceType == null)
                    {
                        resourceType = this.ResolveNonPrimitiveType(elementType);
                    }
                }
            }

            return(resourceType);
        }
Пример #2
0
        private static void BuildTypeProperties(ResourceType parentResourceType, IDictionary <Type, ResourceType> knownTypes, IDictionary <ResourceType, List <ResourceType> > childTypes, Queue <ResourceType> unvisitedTypes, IEnumerable <ResourceSet> entitySets)
        {
            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

            if (parentResourceType.BaseType != null)
            {
                bindingFlags |= BindingFlags.DeclaredOnly;
            }
            HashSet <string> set    = new HashSet <string>(IgnorePropertiesAttribute.GetProperties(parentResourceType.InstanceType, false, bindingFlags), StringComparer.Ordinal);
            HashSet <string> source = new HashSet <string>(LoadETagProperties(parentResourceType), StringComparer.Ordinal);
            ResourceKeyKind  kind   = (ResourceKeyKind)0x7fffffff;

            PropertyInfo[] properties = parentResourceType.InstanceType.GetProperties(bindingFlags);
            if (!properties.Any <PropertyInfo>() && (parentResourceType.BaseType == null))
            {
                throw new NotSupportedException(System.Data.Services.Strings.ReflectionProvider_ResourceTypeHasNoPublicallyVisibleProperties(parentResourceType.FullName));
            }
            foreach (PropertyInfo info in properties)
            {
                if (!set.Contains(info.Name))
                {
                    ResourceType collectionResourceType;
                    if (!info.CanRead || (info.GetIndexParameters().Length != 0))
                    {
                        throw new InvalidOperationException(System.Data.Services.Strings.ReflectionProvider_InvalidProperty(info.Name, parentResourceType.FullName));
                    }
                    ResourcePropertyKind collection = (ResourcePropertyKind)(-1);
                    Type propertyType = info.PropertyType;
                    bool flag         = false;
                    if (!BaseServiceProvider.TryGetType(knownTypes, propertyType, out collectionResourceType))
                    {
                        Type iEnumerableElement = BaseServiceProvider.GetIEnumerableElement(info.PropertyType);
                        if (iEnumerableElement != null)
                        {
                            BaseServiceProvider.TryGetType(knownTypes, iEnumerableElement, out collectionResourceType);
                            flag         = true;
                            propertyType = iEnumerableElement;
                        }
                    }
                    if (collectionResourceType != null)
                    {
                        if (collectionResourceType.ResourceTypeKind == ResourceTypeKind.Primitive)
                        {
                            if (flag)
                            {
                                collection = ResourcePropertyKind.Collection;
                            }
                            else
                            {
                                ResourceKeyKind kind3;
                                if (((parentResourceType.BaseType == null) && (parentResourceType.ResourceTypeKind == ResourceTypeKind.EntityType)) && IsPropertyKeyProperty(info, out kind3))
                                {
                                    if (kind3 < kind)
                                    {
                                        if (parentResourceType.KeyProperties.Count != 0)
                                        {
                                            parentResourceType.RemoveKeyProperties();
                                        }
                                        kind       = kind3;
                                        collection = ResourcePropertyKind.Key | ResourcePropertyKind.Primitive;
                                    }
                                    else if (kind3 == kind)
                                    {
                                        collection = ResourcePropertyKind.Key | ResourcePropertyKind.Primitive;
                                    }
                                    else
                                    {
                                        collection = ResourcePropertyKind.Primitive;
                                    }
                                }
                                else
                                {
                                    collection = ResourcePropertyKind.Primitive;
                                }
                            }
                        }
                        else if (collectionResourceType.ResourceTypeKind == ResourceTypeKind.ComplexType)
                        {
                            collection = flag ? ResourcePropertyKind.Collection : ResourcePropertyKind.ComplexType;
                        }
                        else if (collectionResourceType.ResourceTypeKind == ResourceTypeKind.EntityType)
                        {
                            collection = flag ? ResourcePropertyKind.ResourceSetReference : ResourcePropertyKind.ResourceReference;
                        }
                    }
                    else
                    {
                        collectionResourceType = IsEntityOrComplexType(propertyType, knownTypes, childTypes, unvisitedTypes);
                        if (collectionResourceType != null)
                        {
                            if (collectionResourceType.ResourceTypeKind == ResourceTypeKind.ComplexType)
                            {
                                if (flag)
                                {
                                    if (BaseServiceProvider.GetIEnumerableElement(propertyType) != null)
                                    {
                                        throw new InvalidOperationException(System.Data.Services.Strings.ReflectionProvider_CollectionOfCollectionProperty(info.Name, parentResourceType.FullName));
                                    }
                                    collection = ResourcePropertyKind.Collection;
                                }
                                else
                                {
                                    collection = ResourcePropertyKind.ComplexType;
                                }
                            }
                            else
                            {
                                collection = flag ? ResourcePropertyKind.ResourceSetReference : ResourcePropertyKind.ResourceReference;
                            }
                        }
                    }
                    if ((collectionResourceType == null) || ((collectionResourceType.ResourceTypeKind == ResourceTypeKind.EntityType) && (parentResourceType.ResourceTypeKind == ResourceTypeKind.ComplexType)))
                    {
                        if (collectionResourceType != null)
                        {
                            throw new InvalidOperationException(System.Data.Services.Strings.ReflectionProvider_ComplexTypeWithNavigationProperty(info.Name, parentResourceType.FullName));
                        }
                        if (flag && (BaseServiceProvider.GetIEnumerableElement(propertyType) != null))
                        {
                            throw new InvalidOperationException(System.Data.Services.Strings.ReflectionProvider_CollectionOfCollectionProperty(info.Name, parentResourceType.FullName));
                        }
                        if (flag)
                        {
                            throw new InvalidOperationException(System.Data.Services.Strings.ReflectionProvider_CollectionOfUnsupportedTypeProperty(info.Name, parentResourceType.FullName, propertyType));
                        }
                        if (CommonUtil.IsUnsupportedType(propertyType))
                        {
                            throw new InvalidOperationException(System.Data.Services.Strings.BadProvider_UnsupportedPropertyType(info.Name, parentResourceType.FullName));
                        }
                        throw new InvalidOperationException(System.Data.Services.Strings.ReflectionProvider_InvalidProperty(info.Name, parentResourceType.FullName));
                    }
                    if ((collectionResourceType.ResourceTypeKind == ResourceTypeKind.EntityType) && (InternalGetContainerForResourceType(propertyType, entitySets) == null))
                    {
                        throw new InvalidOperationException(System.Data.Services.Strings.ReflectionProvider_EntityPropertyWithNoEntitySet(parentResourceType.FullName, info.Name));
                    }
                    if (collection == ResourcePropertyKind.Collection)
                    {
                        collectionResourceType = ResourceType.GetCollectionResourceType(collectionResourceType);
                    }
                    if (source.Remove(info.Name))
                    {
                        collection |= ResourcePropertyKind.ETag;
                    }
                    ResourceProperty  property          = new ResourceProperty(info.Name, collection, collectionResourceType);
                    MimeTypeAttribute mimeTypeAttribute = MimeTypeAttribute.GetMimeTypeAttribute(info);
                    if (mimeTypeAttribute != null)
                    {
                        property.MimeType = mimeTypeAttribute.MimeType;
                    }
                    parentResourceType.AddProperty(property);
                }
            }
            if ((parentResourceType.ResourceTypeKind == ResourceTypeKind.EntityType) && ((parentResourceType.KeyProperties == null) || (parentResourceType.KeyProperties.Count == 0)))
            {
                throw new InvalidOperationException(System.Data.Services.Strings.ReflectionProvider_KeyPropertiesCannotBeIgnored(parentResourceType.FullName));
            }
            if (source.Count != 0)
            {
                throw new InvalidOperationException(System.Data.Services.Strings.ReflectionProvider_ETagPropertyNameNotValid(source.ElementAt <string>(0), parentResourceType.FullName));
            }
        }