/// <summary> /// Checks if the given type is assignable to this type. In other words, if this type /// is a subtype of the given type or not. /// </summary> /// <param name="subType">resource type to check.</param> /// <returns>true, if the given type is assignable to this type. Otherwise returns false.</returns> internal static bool IsAssignableFrom(this ResourceType thisType, ResourceType subType) { CollectionResourceType thisCollectionType = thisType as CollectionResourceType; CollectionResourceType subCollectionType = subType as CollectionResourceType; if (thisCollectionType != null && subCollectionType != null) { return(thisCollectionType.ItemType.IsAssignableFrom(subCollectionType.ItemType)); } EntityCollectionResourceType thisEntityCollectionType = thisType as EntityCollectionResourceType; EntityCollectionResourceType subEntityCollectionType = subType as EntityCollectionResourceType; if (thisEntityCollectionType != null && subEntityCollectionType != null) { return(thisEntityCollectionType.ItemType.IsAssignableFrom(subEntityCollectionType.ItemType)); } while (subType != null) { if (subType == thisType) { return(true); } subType = subType.BaseType; } return(false); }
protected ODataCollectionValue GetCollection(string propertyName, CollectionResourceType propertyResourceType, object propertyValue) { Func <object, ODataComplexValue> valueConverter = null; this.RecurseEnter(); IEnumerable collectionEnumerable = GetCollectionEnumerable(propertyValue, propertyName); ODataCollectionValue value2 = new ODataCollectionValue { TypeName = propertyResourceType.FullName }; if (propertyResourceType.ItemType.ResourceTypeKind == ResourceTypeKind.Primitive) { value2.Items = GetEnumerable <object>(collectionEnumerable, new Func <object, object>(Serializer.GetPrimitiveValue)); } else { if (valueConverter == null) { valueConverter = value => this.GetComplexValue(propertyName, value); } value2.Items = GetEnumerable <ODataComplexValue>(collectionEnumerable, valueConverter); } this.RecurseLeave(); return(value2); }
private static void GetAllDependencies(ResourceType resourceType, HashSet <ResourceType> dependencies) { foreach (ResourceProperty property in resourceType.Properties) { ResourceType itemType = null; if (property.ResourceType.ResourceTypeKind != ResourceTypeKind.ComplexType) { if (property.ResourceType.ResourceTypeKind == ResourceTypeKind.Collection) { CollectionResourceType collectionResourceType = property.ResourceType as CollectionResourceType; if (collectionResourceType.ItemType.ResourceTypeKind != ResourceTypeKind.Primitive) { itemType = collectionResourceType.ItemType; } } } else { itemType = property.ResourceType; } if (itemType == null || dependencies.Any <ResourceType>((ResourceType item) => item.FullName == itemType.FullName)) { continue; } dependencies.Add(itemType); ResourceTypeExtensions.GetAllDependencies(itemType, dependencies); } }
/// <summary> /// Get's the MarkDown for a type /// </summary> /// <returns></returns> public string GetMarkDown() { if (this.IsObject) { string typeName = CustomTypeName.TypeOnly(); return($"[{typeName}]({typeName}.md)"); } if (this.IsCollection) { if (!string.IsNullOrEmpty(this.CustomTypeName)) { string typeName = CustomTypeName.TypeOnly(); return($"[{typeName}]({typeName}.md) collection"); } if (CollectionResourceType == SimpleDataType.Object) { string typeName = CollectionResourceType.ODataResourceName().TypeOnly(); return($"[{typeName}]({typeName}.md) collection"); } else { return($"{CollectionResourceType} collection"); } } // Primitive type return(this.Type.ToString()); }
public void SetValue(object targetResource, string propertyName, object propertyValue) { T instance = this.tokens[(int)targetResource]; ResourceType resourceType = this.GetResourceType(instance); if (resourceType != null) { ResourceProperty resourceProperty = resourceType.Properties.SingleOrDefault(rp => rp.Name == propertyName); if (resourceProperty != null && resourceProperty.Kind == ResourcePropertyKind.Collection) { CollectionResourceType collectionType = resourceProperty.ResourceType as CollectionResourceType; IList list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(collectionType.ItemType.InstanceType)); foreach (var i in (IEnumerable)propertyValue) { object itemValue = i; if (collectionType.ItemType.ResourceTypeKind == ResourceTypeKind.ComplexType) { itemValue = this.tokens[(int)itemValue]; } list.Add(itemValue); } propertyValue = list; } } this.SetPropertyValue(instance, propertyName, propertyValue); }
public override object Serialize(object clrObject, int depth) { CollectionResourceType resourceType = base.ResourceType as CollectionResourceType; if (clrObject as PSObject != null) { clrObject = (clrObject as PSObject).BaseObject; } if (clrObject != null) { Type type = clrObject.GetType(); if (!TypeSystem.ContainsDictionaryInterface(type)) { if (!TypeSystem.ContainsEnumerableInterface(type)) { object[] assemblyQualifiedName = new object[1]; assemblyQualifiedName[0] = type.AssemblyQualifiedName; throw new ArgumentException(ExceptionHelpers.GetExceptionMessage(Resources.CollectionSeralizationFailedNotValidCollectionType, assemblyQualifiedName)); } else { return(this.SerializeEnumerable(clrObject as IEnumerable, resourceType, depth)); } } else { return(this.SerializeDictionary(clrObject as IDictionary, resourceType, depth)); } } else { return(this.SerializeEnumerable(null, resourceType, depth)); } }
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); }
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); }
private object ConvertCollection(ODataCollectionValue collection, ResourceType resourceType) { CollectionResourceType type = resourceType as CollectionResourceType; IList list = Deserializer.CreateNewCollection(); base.RecurseEnter(); foreach (object obj2 in collection.Items) { ResourceType itemType = type.ItemType; list.Add(this.ConvertValue(obj2, ref itemType)); } base.RecurseLeave(); return(Deserializer.GetReadOnlyCollection(list)); }
internal IEnumerable SerializeDictionary(IDictionary dictionary, CollectionResourceType collectionResourceType, int depth) { ResourceType itemType = collectionResourceType.ItemType; Type[] instanceType = new Type[1]; instanceType[0] = itemType.InstanceType; Type[] typeArray = instanceType; Type type = typeof(List <>); Type type1 = type.MakeGenericType(typeArray); IList lists = (IList)Activator.CreateInstance(type1); foreach (object key in dictionary.Keys) { KeyValuePair <object, object> keyValuePair = new KeyValuePair <object, object>(key, dictionary[key]); lists.Add(SerializerBase.SerializeResourceType(keyValuePair, itemType, depth)); } return(lists); }
internal IEnumerable SerializeEnumerable(IEnumerable clrObjects, CollectionResourceType collectionResourceType, int depth) { ResourceType itemType = collectionResourceType.ItemType; Type[] instanceType = new Type[1]; instanceType[0] = itemType.InstanceType; Type[] typeArray = instanceType; Type type = typeof(List <>); Type type1 = type.MakeGenericType(typeArray); IList lists = (IList)Activator.CreateInstance(type1); if (clrObjects != null) { foreach (object clrObject in clrObjects) { lists.Add(SerializerBase.SerializeResourceType(clrObject, itemType, depth)); } } return(lists); }
/// <summary> /// Converts the collection reported by OData reader into WCF DS collection resource. /// </summary> /// <param name="collection">The collection reported by the reader.</param> /// <param name="resourceType">The expected collection resource type.</param> /// <returns>THe newly created WCF DS collection resource.</returns> private object ConvertCollection(ODataCollectionValue collection, ResourceType resourceType) { Debug.Assert(collection != null, "collection != null"); if (resourceType == null) { // Open collection property value - read the type from the value Debug.Assert(!string.IsNullOrEmpty(collection.TypeName), "ODataLib should have verified that open collection property value has a type name since we provided metadata."); string itemTypeName = CommonUtil.GetCollectionItemTypeName(collection.TypeName, false); var itemType = PrimitiveResourceTypeMap.TypeMap.GetPrimitive(itemTypeName); itemType = itemType ?? this.Service.Provider.TryResolveResourceType(itemTypeName); Debug.Assert(itemType != null, "The item Type in open collection property can not be resolved."); resourceType = CollectionResourceType.GetCollectionResourceType(itemType); Debug.Assert(resourceType.ResourceTypeKind == ResourceTypeKind.Collection, "ODataLib should have verified that collection value has a collection resource type."); } Debug.Assert( collection.TypeName == null || collection.TypeName == resourceType.FullName, "We don't support any inheritance in collections, so the type of the collection should exactly match the type in the metadata."); CollectionResourceType collectionResourceType = resourceType as CollectionResourceType; Debug.Assert(collectionResourceType != null, "The resource type for collection must be a CollectionResourceType."); IList collectionList = Deserializer.CreateNewCollection(); this.RecurseEnter(); Debug.Assert(collection.Items != null, "The ODataLib reader should always populate the ODataCollectionValue.Items collection."); foreach (object odataItem in collection.Items) { ResourceType itemType = collectionResourceType.ItemType; collectionList.Add(this.ConvertValue(odataItem, ref itemType)); } this.RecurseLeave(); return(Deserializer.GetReadOnlyCollection(collectionList, collectionResourceType)); }