예제 #1
0
 /// <summary>
 /// Pushes an collection access on the path.
 /// </summary>
 /// <param name="descriptor">The descriptor of the collection.</param>
 /// <param name="index">The index in the collection.</param>
 /// <exception cref="System.ArgumentNullException">descriptor</exception>
 public void Push(CollectionDescriptor descriptor, int index)
 {
     if (descriptor == null)
     {
         throw new ArgumentNullException(nameof(descriptor));
     }
     AddItem(new CollectionPathItem(descriptor, index));
 }
예제 #2
0
 public CollectionPathItem(CollectionDescriptor descriptor, int index)
 {
     if (descriptor == null)
     {
         throw new ArgumentNullException(nameof(descriptor));
     }
     Descriptor = descriptor;
     Index      = index;
 }
예제 #3
0
        /// <inheritdoc />
        public virtual void VisitCollection(IEnumerable collection, CollectionDescriptor descriptor)
        {
            var i = 0;

            // Make a copy in case VisitCollectionItem mutates something
            var items = collection.Cast <object>().ToList();

            foreach (var item in items)
            {
                CurrentPath.Push(descriptor, i);
                VisitCollectionItem(collection, descriptor, i, item, TypeDescriptorFactory.Find(item?.GetType() ?? descriptor.ElementType));
                CurrentPath.Pop();
                i++;
            }
        }
예제 #4
0
        /// <summary>
        /// Creates a type descriptor for the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>An instance of type descriptor.</returns>
        protected virtual ITypeDescriptor Create(Type type)
        {
            ITypeDescriptor descriptor;

            // The order of the descriptors here is important

            if (PrimitiveDescriptor.IsPrimitive(type))
            {
                descriptor = new PrimitiveDescriptor(this, type, emitDefaultValues, namingConvention);
            }
            else if (DictionaryDescriptor.IsDictionary(type)) // resolve dictionary before collections, as they are also collections
            {
                // IDictionary
                descriptor = new DictionaryDescriptor(this, type, emitDefaultValues, namingConvention);
            }
            else if (CollectionDescriptor.IsCollection(type))
            {
                // ICollection
                descriptor = new CollectionDescriptor(this, type, emitDefaultValues, namingConvention);
            }
            else if (type.IsArray)
            {
                if (type.GetArrayRank() == 1 && !type.GetElementType().IsArray)
                {
                    // array[] - only single dimension array is supported
                    descriptor = new ArrayDescriptor(this, type, emitDefaultValues, namingConvention);
                }
                else
                {
                    // multi-dimension array to be treated as a 'standard' object
                    descriptor = new NotSupportedObjectDescriptor(this, type, emitDefaultValues, namingConvention);
                }
            }
            else if (NullableDescriptor.IsNullable(type))
            {
                descriptor = new NullableDescriptor(this, type, emitDefaultValues, namingConvention);
            }
            else
            {
                // standard object (class or value type)
                descriptor = new ObjectDescriptor(this, type, emitDefaultValues, namingConvention);
            }

            return(descriptor);
        }
예제 #5
0
        /// <inheritdoc />
        public virtual void VisitCollection(IEnumerable collection, CollectionDescriptor descriptor)
        {
            if (descriptor.Category == DescriptorCategory.Set)
            {
                throw new ArgumentException("Shouldn't call VisitCollection() to visit a set");
            }
            var i = 0;

            // Make a copy in case VisitCollectionItem mutates something
            var items = collection.Cast <object>().ToList();

            foreach (var item in items)
            {
                CurrentPath.Push(descriptor, i);
                VisitCollectionItem(collection, descriptor, i, item, TypeDescriptorFactory.Find(item?.GetType() ?? descriptor.ElementType));
                CurrentPath.Pop();
                i++;
            }
        }
예제 #6
0
 /// <inheritdoc />
 public virtual void VisitCollectionItem(IEnumerable collection, CollectionDescriptor descriptor, int index, object item, ITypeDescriptor itemDescriptor)
 {
     Visit(item, itemDescriptor);
 }