public static DictionaryAdapterMeta GetAdapterMeta(this DictionaryAdapterMeta source, Type type)
		{
			var descriptor = new DictionaryDescriptor(GetSharedBehaviors(source));
			descriptor.AddInitializers    (source.Initializers);
			descriptor.AddMetaInitializers(source.MetaInitializers);

			return source.Factory.GetAdapterMeta(type, descriptor);
		}	
        public override void VisitDictionaryKeyValue(object dictionary, DictionaryDescriptor descriptor, object key, ITypeDescriptor keyDescriptor, object value, ITypeDescriptor valueDescriptor)
        {
            // TODO: CurrentPath is valid only for value, not key
            //if (ProcessObject(key, keyDescriptor.Type)) key = null;
            if (ProcessObject(value, valueDescriptor.Type)) return;

            Visit(value, valueDescriptor);
            //base.VisitDictionaryKeyValue(dictionary, descriptor, key, keyDescriptor, value, valueDescriptor);
        }
		public static object CreateChildAdapter(this IDictionaryAdapter parent, Type type, XmlAdapter adapter, IDictionary dictionary)
		{
			if (null == dictionary)
				dictionary = new Hashtable();

			var descriptor = new DictionaryDescriptor(GetSharedBehaviors(parent.Meta));
		    parent.This.Descriptor.CopyBehaviors(descriptor);
		    descriptor.AddBehavior(adapter);

		    return parent.This.Factory.GetAdapter(type, dictionary, descriptor);
		}
 public override void VisitDictionary(object dictionary, DictionaryDescriptor descriptor)
 {
     CollectionItemIdentifiers itemIds;
     if (inNonIdentifiableType == 0 && !CollectionItemIdHelper.TryGetCollectionItemIds(dictionary, out itemIds))
     {
         itemIds = CollectionItemIdHelper.GetCollectionItemIds(dictionary);
         foreach (var element in descriptor.GetEnumerator(dictionary))
         {
             itemIds.Add(element.Key, ItemId.New());
         }
     }
     base.VisitDictionary(dictionary, descriptor);
 }
 /// <inheritdoc/>
 public override void VisitDictionaryKeyValue(object dictionary, DictionaryDescriptor descriptor, object key, ITypeDescriptor keyDescriptor, object value, ITypeDescriptor valueDescriptor)
 {
     if (CurrentPath.Match(MemberPath))
     {
         var keyValueType = typeof(KeyValuePair<,>).MakeGenericType(keyDescriptor.Type, valueDescriptor.Type);
         var keyValueDescriptor = TypeDescriptorFactory.Find(keyValueType);
         var keyValuePair = Activator.CreateInstance(keyValueType, key, value);
         VisitAssetMember(keyValuePair, keyValueDescriptor);
     }
     else
     {
         base.VisitDictionaryKeyValue(dictionary, descriptor, key, keyDescriptor, value, valueDescriptor);
     }
 }
        /// <summary>
        /// Initialize the tests.
        /// </summary>
        public virtual void Initialize()
        {
            TypeFactory = new TypeDescriptorFactory();
            var myClassDesc = TypeFactory.Find(typeof(MyClass));
            var myStructDesc = TypeFactory.Find(typeof(MyStruct));
            ListClassDesc = (CollectionDescriptor)TypeFactory.Find(typeof(List<MyClass>));
            MapClassDesc = (DictionaryDescriptor)TypeFactory.Find(typeof(Dictionary<string, MyClass>));

            MemberValue = myClassDesc.Members.FirstOrDefault(member => member.Name == "Value");
            MemberSub = myClassDesc.Members.FirstOrDefault(member => member.Name == "Sub");
            MemberStruct = myClassDesc.Members.FirstOrDefault(member => member.Name == "Struct");
            MemberSubs = myClassDesc.Members.FirstOrDefault(member => member.Name == "Subs");
            MemberMaps = myClassDesc.Members.FirstOrDefault(member => member.Name == "Maps");
            MemberX = myStructDesc.Members.FirstOrDefault(member => member.Name == "X");
            MemberClass = myStructDesc.Members.FirstOrDefault(member => member.Name == "Class");
        }
        /// <inheritdoc/>
        public override void VisitDictionary(object dictionary, DictionaryDescriptor descriptor)
        {
            var containerNode = GetContextNode();

            if (!IsPrimitiveType(descriptor.KeyType))
                throw new QuantumConsistencyException("A dictionary with a primary type for keys", null, "A dictionary [{0}] for keys", descriptor.KeyType.FullName, containerNode);

            // TODO: an access to the count function in DictionaryDescriptor
            var count = ((IEnumerable)dictionary).Cast<object>().Count();
            var referenceInfo = GetReferenceInfo(descriptor.Type, dictionary);
            if (referenceInfo != null && referenceInfo.EnumerableCount != count)
                throw new QuantumConsistencyException("A node with an EnumerableReference containing [{0}] items", referenceInfo.EnumerableCount.ToStringSafe(), "A node with an EnumerableReference containing [{0}] items", count.ToStringSafe(), containerNode);

            if (IsPrimitiveType(descriptor.ValueType, false) || IsEnumerable(descriptor.ValueType))
            {
                base.VisitDictionary(dictionary, descriptor);
            }
        }
		public void CanFetchProperties()
		{
			var getter = new CustomGetter();
			var custom = new DictionaryDescriptor().AddGetter(getter);
			factory.GetAdapter(typeof(IPhone), dictionary, custom);

			Assert.AreEqual(1, getter.PropertiesFetched.Count);

			Assert.AreEqual(1, getter.PropertiesFetched.Count);
			Assert.IsTrue(getter.PropertiesFetched.Contains("Number"));
		}
        public override void VisitDictionaryKeyValue(object dictionary, DictionaryDescriptor descriptor, object key, ITypeDescriptor keyDescriptor, object value, ITypeDescriptor valueDescriptor)
        {
            var node = stackItems.Peek();
            // TODO modify DataVisitorBase to allow only IDictionary?
            var newNode = new DataVisitDictionaryItem(key, keyDescriptor, value, valueDescriptor);
            AddItem(node, newNode);

            stackItems.Push(newNode);
            base.VisitDictionaryKeyValue(dictionary, descriptor, key, keyDescriptor, value, valueDescriptor);
            stackItems.Pop();
        }
 public override void VisitDictionaryKeyValue(object dictionaryObj, DictionaryDescriptor descriptor, object key, ITypeDescriptor keyDescriptor, object value, ITypeDescriptor valueDescriptor)
 {
     base.VisitDictionaryKeyValue(dictionaryObj, descriptor, key, keyDescriptor, value, valueDescriptor);
     var assetReference = value as AssetReference;
     var assetBase = value as AssetBase;
     var attachedReference = value != null ? AttachedReferenceManager.GetAttachedReference(value) : null;
     if (assetReference != null)
     {
         AddLink(assetReference,
             (guid, location) =>
             {
                 var newValue = AssetReference.New(descriptor.ValueType, guid.HasValue ? guid.Value : assetReference.Id, location);
                 descriptor.SetValue(dictionaryObj, key, newValue);
                 return newValue;
             });
     }
     else if (assetBase != null)
     {
         AddLink(assetBase,
             (guid, location) =>
             {
                 var newValue = new AssetBase(location, assetBase.Asset);
                 descriptor.SetValue(dictionaryObj, key, newValue);
                 return newValue;
             });
     }
     else if (attachedReference != null)
     {
         AddLink(attachedReference,
             (guid, location) =>
             {
                 object newValue = guid.HasValue && guid.Value != Guid.Empty ? AttachedReferenceManager.CreateSerializableVersion(descriptor.ValueType, guid.Value, location) : null;
                 descriptor.SetValue(dictionaryObj, key, newValue);
                 return newValue;
             });
     }
     else if (value is UFile)
     {
         AddLink(value,
             (guid, location) =>
             {
                 var newValue = new UFile(location);
                 descriptor.SetValue(dictionaryObj, key, newValue);
                 return newValue;
             });
     }
     else if (value is UDirectory)
     {
         AddLink(value,
             (guid, location) =>
             {
                 var newValue = new UDirectory(location);
                 descriptor.SetValue(dictionaryObj, key, newValue);
                 return newValue;
             });
     }
 }
Example #11
0
 public virtual void VisitDictionaryKeyValue(object dictionary, DictionaryDescriptor descriptor, object key, ITypeDescriptor keyDescriptor, object value, ITypeDescriptor valueDescriptor)
 {
     Visit(key, keyDescriptor);
     Visit(value, valueDescriptor);
 }
Example #12
0
        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 #13
0
        /// <inheritdoc/>
        public override void VisitDictionary(object dictionary, DictionaryDescriptor descriptor)
        {
            if (!IsPrimitiveType(descriptor.KeyType))
                throw new InvalidOperationException("The type of dictionary key must be a primary type.");

            // Don't visit items unless they are primitive or enumerable (collections within collections)
            if (IsPrimitiveType(descriptor.ValueType, false) || IsCollection(descriptor.ValueType))
            {
                base.VisitDictionary(dictionary, descriptor);
            }
        }
Example #14
0
        public virtual void VisitDictionary(object dictionary, DictionaryDescriptor descriptor)
        {
            foreach (var keyValue in descriptor.GetEnumerator(dictionary))
            {
                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();
            }
        }
        /// <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(attributeRegistry, type);
            }
            else if (DictionaryDescriptor.IsDictionary(type)) // resolve dictionary before collections, as they are also collections
            {
                // IDictionary
                descriptor = new DictionaryDescriptor(attributeRegistry, type, emitDefaultValues);
            }
            else if (CollectionDescriptor.IsCollection(type))
            {
                // ICollection
                descriptor = new CollectionDescriptor(attributeRegistry, type, emitDefaultValues);
            }
            else if (type.IsArray)
            {
                // array[]
                descriptor = new ArrayDescriptor(attributeRegistry, type);
            }
            else if (NullableDescriptor.IsNullable(type))
            {
                descriptor = new NullableDescriptor(attributeRegistry, type);
            }
            else
            {
                // standard object (class or value type)
                descriptor = new ObjectDescriptor(attributeRegistry, type, emitDefaultValues);
            }
            return descriptor;
        }
 public override void VisitDictionary(object dictionary, DictionaryDescriptor descriptor)
 {
     Fixup(dictionary);
     base.VisitDictionary(dictionary, descriptor);
 }