public static TableResourceContainer GetUtilityRowResourceContainer(string accountName, string tableName) { System.Data.Services.Providers.ResourceType resourceType = new System.Data.Services.Providers.ResourceType(typeof(UtilityRow), ResourceTypeKind.EntityType, null, accountName, tableName, false) { CanReflectOnInstanceType = false, IsOpenType = true }; ResourceProperty utilityRowResourceProperty = new UtilityRowResourceProperty("PartitionKey", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, System.Data.Services.Providers.ResourceType.GetPrimitiveResourceType(typeof(string))) { CanReflectOnInstanceTypeProperty = false }; resourceType.AddProperty(utilityRowResourceProperty); ResourceProperty resourceProperty = new UtilityRowResourceProperty("RowKey", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, System.Data.Services.Providers.ResourceType.GetPrimitiveResourceType(typeof(string))) { CanReflectOnInstanceTypeProperty = false }; resourceType.AddProperty(resourceProperty); ResourceProperty utilityRowResourceProperty1 = new UtilityRowResourceProperty("Timestamp", ResourcePropertyKind.Primitive | ResourcePropertyKind.ETag, System.Data.Services.Providers.ResourceType.GetPrimitiveResourceType(typeof(DateTime))) { CanReflectOnInstanceTypeProperty = false }; resourceType.AddProperty(utilityRowResourceProperty1); TableResourceContainer tableResourceContainer = new TableResourceContainer(tableName, resourceType); tableResourceContainer.SetReadOnly(); return(tableResourceContainer); }
internal void AddComplexCollectionProperty(ResourceType resourceType, string name, ResourceType complexType) { CollectionResourceType collectionResourceType = ResourceType.GetCollectionResourceType(complexType); ResourceProperty resourcePropertyWithDescription = new ResourcePropertyWithDescription(name, ResourcePropertyKind.Collection, collectionResourceType); resourcePropertyWithDescription.CanReflectOnInstanceTypeProperty = false; PropertyCustomState propertyCustomState = new PropertyCustomState(); resourcePropertyWithDescription.CustomState = propertyCustomState; resourceType.AddProperty(resourcePropertyWithDescription); }
public static TableResourceContainer GetUtilityTableResourceContainer(string accountName, bool PremiumTableAccountRequest) { System.Data.Services.Providers.ResourceType resourceType = new System.Data.Services.Providers.ResourceType(typeof(UtilityTable), ResourceTypeKind.EntityType, null, accountName, "Tables", false) { CanReflectOnInstanceType = false, IsOpenType = true }; if (PremiumTableAccountRequest) { resourceType.AddProperty(TableResourceContainer.ProvisionedIOPSProperty); resourceType.AddProperty(TableResourceContainer.TableStatusProperty); resourceType.AddProperty(TableResourceContainer.RequestedIOPSProperty); } ResourceProperty utilityTableResourceProperty = new UtilityTableResourceProperty("TableName", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, System.Data.Services.Providers.ResourceType.GetPrimitiveResourceType(typeof(string))) { CanReflectOnInstanceTypeProperty = false }; resourceType.AddProperty(utilityTableResourceProperty); TableResourceContainer tableResourceContainer = new TableResourceContainer("Tables", resourceType); tableResourceContainer.SetReadOnly(); return(tableResourceContainer); }
internal void AddComplexProperty(ResourceType resourceType, string name, ResourceType complexType) { if (complexType.ResourceTypeKind == ResourceTypeKind.ComplexType) { ResourceProperty resourcePropertyWithDescription = new ResourcePropertyWithDescription(name, ResourcePropertyKind.ComplexType, complexType); resourcePropertyWithDescription.CanReflectOnInstanceTypeProperty = false; PropertyCustomState propertyCustomState = new PropertyCustomState(); resourcePropertyWithDescription.CustomState = propertyCustomState; resourceType.AddProperty(resourcePropertyWithDescription); return; } else { throw new InvalidResourceTypeException(complexType.Name, complexType.ResourceTypeKind.ToString(), ResourceTypeKind.ComplexType.ToString()); } }
/// <summary>Adds a key property to the specified <paramref name="resourceType"/>.</summary> /// <param name="resourceType">The resource type to add the property to.</param> /// <param name="name">The name of the property to add.</param> /// <param name="propertyType">The CLR type of the property to add. This can be only a primitive type.</param> /// <param name="isKey">true if the property should be a key property.</param> private void AddPrimitiveProperty(ResourceType resourceType, string name, Type propertyType, bool isKey) { ResourceType type = ResourceType.GetPrimitiveResourceType(propertyType); ResourcePropertyKind kind = ResourcePropertyKind.Primitive; if (isKey) { kind |= ResourcePropertyKind.Key; } ResourceProperty property = new ResourceProperty(name, kind, type); property.CanReflectOnInstanceTypeProperty = false; resourceType.AddProperty(property); }
/// <summary>Adds a collection of complex or primitive items property.</summary> /// <param name="resourceType">The resource type to add the property to.</param> /// <param name="name">The name of the property to add.</param> /// <param name="itemType">The resource type of the item in the collection.</param> public void AddCollectionProperty(ResourceType resourceType, string name, ResourceType itemType) { ResourceProperty property = new ResourceProperty( name, ResourcePropertyKind.Collection, ResourceType.GetCollectionResourceType(itemType)); property.CanReflectOnInstanceTypeProperty = false; resourceType.AddProperty(property); }
/// <summary>Adds a complex property to the specified <paramref name="resourceType"/>.</summary> /// <param name="resourceType">The resource type to add the property to.</param> /// <param name="name">The name of the property to add.</param> /// <param name="complexType">Complex type to use for the property.</param> public void AddComplexProperty(ResourceType resourceType, string name, ResourceType complexType) { if (complexType.ResourceTypeKind != ResourceTypeKind.ComplexType) { throw new ArgumentException("The specified type for the complex property is not a complex type."); } PropertyInfo propertyInfo = resourceType.InstanceType.GetProperty(name, BindingFlags.Public | BindingFlags.Instance); if (propertyInfo == null) { throw new ArgumentException("Can't add a property which does not exist on the instance type."); } if (propertyInfo.PropertyType != complexType.InstanceType) { throw new ArgumentException("The resource type for the complex property doesn't match the instance type of the property."); } ResourceProperty property = new ResourceProperty(name, ResourcePropertyKind.ComplexType, complexType); property.CanReflectOnInstanceTypeProperty = true; property.CustomState = new ResourcePropertyAnnotation() { InstanceProperty = propertyInfo }; resourceType.AddProperty(property); }
/// <summary>Adds a key property to the specified <paramref name="resourceType"/>.</summary> /// <param name="resourceType">The resource type to add the property to.</param> /// <param name="name">The name of the property to add.</param> /// <param name="typeName">The CLR type of the property to add. This can be only a primitive type.</param> /// <param name="isKey">true if the property should be a key property.</param> private void AddPrimitiveProperty(ResourceType resourceType, string name, bool isKey) { PropertyInfo propertyInfo = resourceType.InstanceType.GetProperty(name, BindingFlags.Public | BindingFlags.Instance); if (propertyInfo == null) { throw new ArgumentException("Can't add a property which does not exist on the instance type."); } ResourceType type = ResourceType.GetPrimitiveResourceType(propertyInfo.PropertyType); ResourcePropertyKind kind = ResourcePropertyKind.Primitive; if (isKey) { kind |= ResourcePropertyKind.Key; } ResourceProperty property = new ResourceProperty(propertyInfo.Name, kind, type); property.CanReflectOnInstanceTypeProperty = true; property.CustomState = new ResourcePropertyAnnotation() { InstanceProperty = propertyInfo }; resourceType.AddProperty(property); }
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)); } }
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)); } }
/// <summary> /// Populates the metadata for the properties of the given resource type /// </summary> /// <param name="parentResourceType">resource type whose properties metadata needs to be populated</param> /// <param name="knownTypes">list of known types</param> /// <param name="childTypes">list of already known types and their immediate children</param> /// <param name="unvisitedTypes">list of unvisited type</param> /// <param name="entitySets">Available entity sets.</param> private static void BuildTypeProperties( ResourceType parentResourceType, IDictionary<Type, ResourceType> knownTypes, IDictionary<ResourceType, List<ResourceType>> childTypes, Queue<ResourceType> unvisitedTypes, IEnumerable<ResourceSet> entitySets) { Debug.Assert(parentResourceType != null, "parentResourceType != null"); Debug.Assert(knownTypes != null, "knownTypes != null"); Debug.Assert(unvisitedTypes != null, "unvisitedTypes != null"); Debug.Assert(entitySets != null, "entitySets != null"); BindingFlags bindingFlags = WebUtil.PublicInstanceBindingFlags; // For non root types, we should only look for properties that are declared for this type if (parentResourceType.BaseType != null) { bindingFlags = bindingFlags | BindingFlags.DeclaredOnly; } HashSet<string> propertiesToBeIgnored = new HashSet<string>(IgnorePropertiesAttribute.GetProperties(parentResourceType.InstanceType, false /*inherit*/, bindingFlags), StringComparer.Ordinal); Debug.Assert(parentResourceType.IsOpenType == false, "ReflectionServiceProvider does not support Open types."); HashSet<string> etagPropertyNames = new HashSet<string>(LoadETagProperties(parentResourceType), StringComparer.Ordinal); ResourceKeyKind keyKind = (ResourceKeyKind)Int32.MaxValue; PropertyInfo[] properties = parentResourceType.InstanceType.GetProperties(bindingFlags); foreach (PropertyInfo property in properties) { // Ignore the properties which are specified in the IgnoreProperties attribute if (propertiesToBeIgnored.Contains(property.Name)) { continue; } if (property.CanRead && property.GetIndexParameters().Length == 0) { ResourcePropertyKind kind = (ResourcePropertyKind)(-1); ResourceKeyKind currentKeyKind = (ResourceKeyKind)(-1); ResourceType resourceType; Type resourcePropertyType = property.PropertyType; ResourceSet container = null; bool collection = false; if (!TryGetType(knownTypes, resourcePropertyType, out resourceType)) { Type collectionType = GetIEnumerableElement(property.PropertyType); if (collectionType != null) { TryGetType(knownTypes, collectionType, out resourceType); // Even if the above method returns false, we should set the // following variable appropriately, so that we can use them below collection = true; resourcePropertyType = collectionType; } } if (resourceType != null) { #region Already Known Type if (resourceType.ResourceTypeKind == ResourceTypeKind.Primitive) { // Check for key property only on root types, since keys must be defined on the root types if (parentResourceType.BaseType == null && parentResourceType.ResourceTypeKind == ResourceTypeKind.EntityType && IsPropertyKeyProperty(property, out currentKeyKind)) { if ((int)currentKeyKind < (int)keyKind) { if (parentResourceType.KeyProperties.Count != 0) { // Remove the existing property as key property - mark it as non key property parentResourceType.RemoveKeyProperties(); } keyKind = currentKeyKind; kind = ResourcePropertyKind.Key | ResourcePropertyKind.Primitive; } else if ((int)currentKeyKind == (int)keyKind) { Debug.Assert(currentKeyKind == ResourceKeyKind.AttributedKey, "This is the only way of specifying composite keys"); kind = ResourcePropertyKind.Key | ResourcePropertyKind.Primitive; } else { kind = ResourcePropertyKind.Primitive; } } else { kind = ResourcePropertyKind.Primitive; } } else if (resourceType.ResourceTypeKind == ResourceTypeKind.ComplexType) { kind = ResourcePropertyKind.ComplexType; } else if (resourceType.ResourceTypeKind == ResourceTypeKind.EntityType) { kind = collection ? ResourcePropertyKind.ResourceSetReference : ResourcePropertyKind.ResourceReference; } #endregion // Already Known Type } else { resourceType = IsEntityOrComplexType(resourcePropertyType, knownTypes, childTypes, unvisitedTypes); if (resourceType != null) { if (resourceType.ResourceTypeKind == ResourceTypeKind.ComplexType) { kind = ResourcePropertyKind.ComplexType; } else { Debug.Assert(resourceType.ResourceTypeKind == ResourceTypeKind.EntityType, "Must be an entity type"); kind = collection ? ResourcePropertyKind.ResourceSetReference : ResourcePropertyKind.ResourceReference; } } } // if resource type is null OR // if resource type is a collection of primitive or complex types OR // if complex type has a property of entity type if (resourceType == null || (resourceType.ResourceTypeKind != ResourceTypeKind.EntityType && collection) || (resourceType.ResourceTypeKind == ResourceTypeKind.EntityType && parentResourceType.ResourceTypeKind == ResourceTypeKind.ComplexType)) { if (resourceType == null) { if (CommonUtil.IsUnsupportedType(resourcePropertyType)) { throw new InvalidOperationException(Strings.BadProvider_UnsupportedPropertyType(property.Name, parentResourceType.FullName)); } throw new InvalidOperationException(Strings.ReflectionProvider_InvalidProperty(property.Name, parentResourceType.FullName)); } else { // collection of complex types not supported throw new InvalidOperationException(Strings.ReflectionProvider_CollectionOfPrimitiveOrComplexNotSupported(property.Name, parentResourceType.FullName)); } } if (resourceType.ResourceTypeKind == ResourceTypeKind.EntityType) { container = InternalGetContainerForResourceType(resourcePropertyType, entitySets); if (container == null) { throw new InvalidOperationException(Strings.ReflectionProvider_EntityPropertyWithNoEntitySet(parentResourceType.FullName, property.Name)); } } if (etagPropertyNames.Remove(property.Name)) { kind |= ResourcePropertyKind.ETag; } ResourceProperty resourceProperty = new ResourceProperty(property.Name, kind, resourceType); MimeTypeAttribute attribute = MimeTypeAttribute.GetMimeTypeAttribute(property); if (attribute != null) { resourceProperty.MimeType = attribute.MimeType; } parentResourceType.AddProperty(resourceProperty); } else { throw new InvalidOperationException(Strings.ReflectionProvider_InvalidProperty(property.Name, parentResourceType.FullName)); } } if (parentResourceType.ResourceTypeKind == ResourceTypeKind.EntityType && (parentResourceType.KeyProperties == null || parentResourceType.KeyProperties.Count == 0)) { throw new InvalidOperationException(Strings.ReflectionProvider_KeyPropertiesCannotBeIgnored(parentResourceType.FullName)); } if (etagPropertyNames.Count != 0) { throw new InvalidOperationException(Strings.ReflectionProvider_ETagPropertyNameNotValid(etagPropertyNames.ElementAt(0), parentResourceType.FullName)); } }
internal void AddResourceSetReferenceProperty(ResourceType sourceType, string name, ResourceType targetType, Schema.AssociationType assocType) { ResourceProperty resourcePropertyWithDescription = new ResourcePropertyWithDescription(name, ResourcePropertyKind.ResourceSetReference, targetType); resourcePropertyWithDescription.CanReflectOnInstanceTypeProperty = false; resourcePropertyWithDescription.CustomState = new ReferenceCustomState(true); resourcePropertyWithDescription.GetReferenceCustomState().AssociationType = assocType; sourceType.AddProperty(resourcePropertyWithDescription); }
internal void AddPrimitiveProperty(ResourceType resourceType, string name, Type propertyType, ResourcePropertyKind flags, object defaultValue) { if (flags == ResourcePropertyKind.Primitive || flags == (ResourcePropertyKind.Primitive | ResourcePropertyKind.ETag) || flags == (ResourcePropertyKind.Primitive | ResourcePropertyKind.Key)) { ResourceType primitiveResourceType = ResourceType.GetPrimitiveResourceType(propertyType); ResourcePropertyKind resourcePropertyKind = ResourcePropertyKind.Primitive; resourcePropertyKind = resourcePropertyKind | flags; ResourceProperty resourcePropertyWithDescription = new ResourcePropertyWithDescription(name, resourcePropertyKind, primitiveResourceType); resourcePropertyWithDescription.CanReflectOnInstanceTypeProperty = false; PropertyCustomState propertyCustomState = new PropertyCustomState(); propertyCustomState.DefaultValue = defaultValue; resourcePropertyWithDescription.CustomState = propertyCustomState; resourceType.AddProperty(resourcePropertyWithDescription); return; } else { throw new ArgumentException(ExceptionHelpers.GetExceptionMessage(Resources.SchemaInvalidKeyOrEtagDiscrepancy, new object[0]), "flags"); } }
internal void AddPrimitiveCollectionProperty(ResourceType resourceType, string name, Type propertyType, object defaultValue) { CollectionResourceType collectionResourceType = ResourceType.GetCollectionResourceType(ResourceType.GetPrimitiveResourceType(propertyType)); ResourceProperty resourcePropertyWithDescription = new ResourcePropertyWithDescription(name, ResourcePropertyKind.Collection, collectionResourceType); resourcePropertyWithDescription.CanReflectOnInstanceTypeProperty = false; PropertyCustomState propertyCustomState = new PropertyCustomState(); propertyCustomState.DefaultValue = defaultValue; resourcePropertyWithDescription.CustomState = propertyCustomState; resourceType.AddProperty(resourcePropertyWithDescription); }
/// <summary>Helper method to add a reference property.</summary> /// <param name="resourceType">The resource type to add the property to.</param> /// <param name="name">The name of the property to add.</param> /// <param name="targetResourceSet">The resource set the resource reference property points to.</param> /// <param name="resourceSetReference">true if the property should be a resource set reference, false if it should be resource reference.</param> private void AddReferenceProperty(ResourceType resourceType, string name, ResourceSet targetResourceSet, bool resourceSetReference) { ResourceProperty property = new ResourceProperty( name, resourceSetReference ? ResourcePropertyKind.ResourceSetReference : ResourcePropertyKind.ResourceReference, targetResourceSet.ResourceType); property.CanReflectOnInstanceTypeProperty = false; resourceType.AddProperty(property); // We don't support type inheritance so the property can only point to the base resource type of the target resource set // We also don't support MEST, that is having two resource sets with the same resource type, so we can determine // the resource set from the resource type. That also means that the property can never point to different resource sets // so we can precreate the ResourceAssociationSet for this property right here as we have all the information. property.CustomState = new ResourcePropertyAnnotation() { ResourceAssociationSet = new ResourceAssociationSet( resourceType.Name + "_" + name + "_" + targetResourceSet.Name, new ResourceAssociationSetEnd(resourceType.GetAnnotation().ResourceSet, resourceType, property), new ResourceAssociationSetEnd(targetResourceSet, targetResourceSet.ResourceType, null)) }; }
/// <summary>Adds a key property to the specified <paramref name="resourceType"/>.</summary> /// <param name="resourceType">The resource type to add the property to.</param> /// <param name="name">The name of the property to add.</param> /// <param name="propertyType">The CLR type of the property to add. This can be only a primitive type.</param> /// <param name="isKey">true if the property should be a key property.</param> private void AddPrimitiveProperty(ResourceType resourceType, string name, Type propertyType, bool isKey) { var type = ResourceType.GetPrimitiveResourceType(propertyType); if (type == null) { throw new ArgumentException(string.Format("Unable to resolve primitive type {0}", propertyType), "propertyType"); } var kind = ResourcePropertyKind.Primitive; if (isKey) { kind |= ResourcePropertyKind.Key; } var property = new ResourceProperty(name, kind, type); property.CanReflectOnInstanceTypeProperty = false; resourceType.AddProperty(property); }
/// <summary>Adds a complex property to the specified <paramref name="resourceType"/>.</summary> /// <param name="resourceType">The resource type to add the property to.</param> /// <param name="name">The name of the property to add.</param> /// <param name="complexType">Complex type to use for the property.</param> public void AddComplexProperty(ResourceType resourceType, string name, ResourceType complexType) { if (complexType.ResourceTypeKind != ResourceTypeKind.ComplexType) { throw new ArgumentException("The specified type for the complex property is not a complex type."); } ResourceProperty property = new ResourceProperty(name, ResourcePropertyKind.ComplexType, complexType); property.CanReflectOnInstanceTypeProperty = false; resourceType.AddProperty(property); }
private static ResourceType CreateResourceType(Type type, ResourceTypeKind kind, ResourceType baseType, IDictionary<Type, ResourceType> knownTypes, IDictionary<ResourceType, List<ResourceType>> childTypes) { ResourceType type2 = new ResourceType(type, kind, baseType, type.Namespace, CommonUtil.GetModelTypeName(type), type.IsAbstract) { IsOpenType = false }; if (type.GetCustomAttributes(typeof(HasStreamAttribute), true).Length == 1) { type2.IsMediaLinkEntry = true; } foreach (object obj2 in type.GetCustomAttributes(typeof(NamedStreamAttribute), baseType == null)) { type2.AddProperty(new ResourceProperty(((NamedStreamAttribute) obj2).Name, ResourcePropertyKind.Stream, ResourceType.PrimitiveResourceTypeMap.GetPrimitive(typeof(Stream)))); } knownTypes.Add(type, type2); childTypes.Add(type2, null); if (baseType != null) { if (childTypes[baseType] == null) { childTypes[baseType] = new List<ResourceType>(); } childTypes[baseType].Add(type2); } return type2; }