private void InitializeCollectionProperties(ResourceType type, ResourceInstance instance, UpdatableToken token, Func<ResourceProperty, bool> filter)
 {
     foreach (ResourceProperty property in type.GetAllPropertiesLazily().Where(filter))
     {
         string propertyName = property.Name;
         Type collectionType = this.GetCollectionPropertyType(type.FullName, propertyName);
         if (collectionType != null)
         {
             var newCollection = Activator.CreateInstance(collectionType);
             token.PendingPropertyUpdates[propertyName] = newCollection;
             this.pendingChanges.Add(() => instance[propertyName] = newCollection);
         }
     }
 }
        /// <summary>
        /// Converts the the property value to the expected property type if it is a declared primitive property.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyValue">The property value.</param>
        /// <param name="targetResourceType">Type of the target resource/entity.</param>
        /// <returns>The converted property value</returns>
        protected virtual object ConvertPropertyValueType(string propertyName, object propertyValue, ResourceType targetResourceType)
        {
            var resourceProperty = targetResourceType.GetAllPropertiesLazily().SingleOrDefault(p => p.Name == propertyName);
            if (resourceProperty == null || !resourceProperty.Kind.HasFlag(ResourcePropertyKind.Primitive))
            {
                return propertyValue;
            }
            else
            {
                if (!resourceProperty.ResourceType.InstanceType.IsAssignableFrom(propertyValue.GetType()))
                {
                    var propertyValueString = propertyValue as string;
                    if (propertyValueString != null)
                    {
                        propertyValue = this.ConvertStringToType(propertyValueString, resourceProperty.ResourceType.InstanceType);
                    }
                    else
                    {
                        propertyValue = Convert.ChangeType(propertyValue, resourceProperty.ResourceType.InstanceType, CultureInfo.InvariantCulture);
                    }
                }
            }

            return propertyValue;
        }