Example #1
0
 /// <summary>
 /// Pushes an dictionary access on the path.
 /// </summary>
 /// <param name="descriptor">The descriptor of the dictionary.</param>
 /// <param name="key">The key.</param>
 /// <exception cref="System.ArgumentNullException">descriptor</exception>
 public void Push(DictionaryDescriptor descriptor, object key)
 {
     if (descriptor == null)
     {
         throw new ArgumentNullException("descriptor");
     }
     AddItem(new DictionaryPathItem(descriptor, key));
 }
Example #2
0
 public DictionaryPathItem(DictionaryDescriptor descriptor, object key)
 {
     if (descriptor == null)
     {
         throw new ArgumentNullException(nameof(descriptor));
     }
     Descriptor = descriptor;
     Key        = key;
 }
Example #3
0
        public virtual void VisitDictionary(object dictionary, DictionaryDescriptor descriptor)
        {
            foreach (var keyValue in descriptor.GetEnumerator(dictionary))
            {
                var key             = keyValue.Key;
                var keyDescriptor   = keyValue.Key == null ? null : TypeDescriptorFactory.Find(keyValue.Key.GetType());
                var value           = keyValue.Value;
                var valueDescriptor = keyValue.Value == null ? null : TypeDescriptorFactory.Find(keyValue.Value.GetType());

                CurrentPath.Push(descriptor, key);
                VisitDictionaryKeyValue(dictionary, descriptor, key, keyDescriptor, value, valueDescriptor);
                CurrentPath.Pop();
            }
        }
Example #4
0
        /// <inheritdoc />
        public virtual void VisitDictionary(object dictionary, DictionaryDescriptor descriptor)
        {
            // Make a copy in case VisitCollectionItem mutates something
            var items = descriptor.GetEnumerator(dictionary).ToList();

            foreach (var keyValue in items)
            {
                var key             = keyValue.Key;
                var keyDescriptor   = TypeDescriptorFactory.Find(keyValue.Key?.GetType() ?? descriptor.KeyType);
                var value           = keyValue.Value;
                var valueDescriptor = TypeDescriptorFactory.Find(keyValue.Value?.GetType() ?? descriptor.ValueType);

                CurrentPath.Push(descriptor, key);
                VisitDictionaryKeyValue(dictionary, descriptor, key, keyDescriptor, value, valueDescriptor);
                CurrentPath.Pop();
            }
        }
Example #5
0
        public static bool IsIdentifiable(Type type)
        {
            bool result;

            lock (IdentifiableTypes)
            {
                if (!IdentifiableTypes.TryGetValue(type, out result))
                {
                    var attributes = TypeDescriptorFactory.Default.AttributeRegistry.GetAttributes(type);

                    // Early exit if we don't need to add a unique identifier to a type
                    result = !(type == typeof(string) ||
                               type.IsValueType ||
                               type.IsArray ||
                               CollectionDescriptor.IsCollection(type) ||
                               DictionaryDescriptor.IsDictionary(type) ||
                               attributes.OfType <NonIdentifiableAttribute>().Any());

                    IdentifiableTypes.Add(type, result);
                }
            }
            return(result);
        }
        /// <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)
            {
                // array[]
                descriptor = new ArrayDescriptor(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);
        }
Example #7
0
 public virtual void VisitDictionaryKeyValue(object dictionary, DictionaryDescriptor descriptor, object key, ITypeDescriptor keyDescriptor, object value, ITypeDescriptor valueDescriptor)
 {
     Visit(key, keyDescriptor);
     Visit(value, valueDescriptor);
 }
Example #8
0
 public DictionaryPathItem(DictionaryDescriptor descriptor, object key)
 {
     Descriptor = descriptor;
     Key        = key;
 }