Ejemplo n.º 1
0
        private void readDictionaryItem(IList <KeyValueItem> items, Type expectedKeyType, Type expectedValueType)
        {
            Property keyProperty   = null;
            Property valueProperty = null;

            foreach (string subElement in _reader.ReadSubElements())
            {
                // check if key and value was found
                if (keyProperty != null && valueProperty != null)
                {
                    break;
                }

                // check if valid tag was found
                PropertyArt propertyArt = getPropertyArtFromString(subElement);
                if (propertyArt == PropertyArt.Unknown)
                {
                    continue;
                }

                // items are as pair key-value defined

                // first is always the key
                if (keyProperty == null)
                {
                    // Key was not defined yet (the first item was found)
                    keyProperty = deserialize(propertyArt, expectedKeyType);
                    continue;
                }

                // key was defined (the second item was found)
                valueProperty = deserialize(propertyArt, expectedValueType);
            }

            // create the item
            var item = new KeyValueItem(keyProperty, valueProperty);

            items.Add(item);
        }
Ejemplo n.º 2
0
        private void readProperties(PropertyCollection properties, Type ownerType)
        {
            foreach (string subElement in _reader.ReadSubElements())
            {
                PropertyArt propertyArt = getPropertyArtFromString(subElement);
                if (propertyArt != PropertyArt.Unknown)
                {
                    // check if the property with the name exists
                    string subPropertyName = _reader.GetAttributeAsString(Attributes.Name);
                    if (string.IsNullOrEmpty(subPropertyName))
                    {
                        continue;
                    }

                    // estimating the propertyInfo
                    PropertyInfo subPropertyInfo = ownerType.GetProperty(subPropertyName);
                    if (subPropertyInfo != null)
                    {
                        Property subProperty = deserialize(propertyArt, subPropertyInfo.PropertyType);
                        properties.Add(subProperty);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private Property deserialize(PropertyArt propertyArt, Type expectedType)
        {
            // Establish the property name
            string propertyName = _reader.GetAttributeAsString(Attributes.Name);

            // Establish the property type
            Type propertyType = _reader.GetAttributeAsType(Attributes.Type);

            // id propertyType is not defined, we'll take the expectedType)
            if (propertyType == null)
            {
                propertyType = expectedType;
            }

            // create the property from the tag
            Property property = Property.CreateInstance(propertyArt, propertyName, propertyType);

            // Null property?
            var nullProperty = property as NullProperty;

            if (nullProperty != null)
            {
                return(nullProperty);
            }

            // is it simple property?
            var simpleProperty = property as SimpleProperty;

            if (simpleProperty != null)
            {
                parseSimpleProperty(_reader, simpleProperty);
                return(simpleProperty);
            }

            // This is not a null property and not a simple property
            // it could be only ReferenceProperty or a reference

            int referenceId = _reader.GetAttributeAsInt(Attributes.ReferenceId);

            // Adding property to cache, it must be done before deserializing the object.
            // Otherwise stack overflow occures if the object references itself
            var referenceTarget = property as ReferenceTargetProperty;

            if (referenceTarget != null && referenceId > 0)
            {
                referenceTarget.Reference = new ReferenceInfo()
                {
                    Id = referenceId, IsProcessed = true
                };
                _propertyCache.Add(referenceId, referenceTarget);
            }

            if (property == null)
            {
                // Property was not created yet, it can be created as a reference from its id
                if (referenceId < 1)
                {
                    // there is no reference, so the property cannot be restored
                    return(null);
                }

                property = createProperty(referenceId, propertyName, propertyType);
                if (property == null)
                {
                    // Reference was not created
                    return(null);
                }

                // property was successfully restored as a reference
                return(property);
            }

            var multiDimensionalArrayProperty = property as MultiDimensionalArrayProperty;

            if (multiDimensionalArrayProperty != null)
            {
                parseMultiDimensionalArrayProperty(multiDimensionalArrayProperty);
                return(multiDimensionalArrayProperty);
            }

            var singleDimensionalArrayProperty = property as SingleDimensionalArrayProperty;

            if (singleDimensionalArrayProperty != null)
            {
                parseSingleDimensionalArrayProperty(singleDimensionalArrayProperty);
                return(singleDimensionalArrayProperty);
            }

            var dictionaryProperty = property as DictionaryProperty;

            if (dictionaryProperty != null)
            {
                parseDictionaryProperty(dictionaryProperty);
                return(dictionaryProperty);
            }

            var collectionProperty = property as CollectionProperty;

            if (collectionProperty != null)
            {
                parseCollectionProperty(collectionProperty);
                return(collectionProperty);
            }

            var complexProperty = property as ComplexProperty;

            if (complexProperty != null)
            {
                parseComplexProperty(complexProperty);
                return(complexProperty);
            }

            return(property);
        }
        private Property deserialize(PropertyArt propertyArt, Type expectedType)
        {
            // Establish the property name
            string propertyName = _reader.GetAttributeAsString(Attributes.Name);

            // Establish the property type
            Type propertyType = _reader.GetAttributeAsType(Attributes.Type);

            // id propertyType is not defined, we'll take the expectedType)
            if (propertyType == null)
            {
                propertyType = expectedType;
            }

            // create the property from the tag
            Property property = Property.CreateInstance(propertyArt, propertyName, propertyType);

            // Null property?
            var nullProperty = property as NullProperty;
            if (nullProperty != null)
            {
                return nullProperty;
            }

            // is it simple property?
            var simpleProperty = property as SimpleProperty;
            if (simpleProperty != null)
            {
                parseSimpleProperty(_reader, simpleProperty);
                return simpleProperty;
            }

            // This is not a null property and not a simple property
            // it could be only ReferenceProperty or a reference

            int referenceId = _reader.GetAttributeAsInt(Attributes.ReferenceId);

            // Adding property to cache, it must be done before deserializing the object.
            // Otherwise stack overflow occures if the object references itself
            var referenceTarget = property as ReferenceTargetProperty;
            if (referenceTarget != null && referenceId > 0)
            {
                referenceTarget.Reference = new ReferenceInfo() {Id = referenceId, IsProcessed = true};
                _propertyCache.Add(referenceId, referenceTarget);
            }

            if (property==null)
            {
                // Property was not created yet, it can be created as a reference from its id
                if (referenceId < 1)
                    // there is no reference, so the property cannot be restored
                    return null;

                property = createProperty(referenceId, propertyName, propertyType);
                if (property == null)
                    // Reference was not created
                    return null;

                // property was successfully restored as a reference
                return property;
            }

            var multiDimensionalArrayProperty = property as MultiDimensionalArrayProperty;
            if (multiDimensionalArrayProperty != null)
            {
                parseMultiDimensionalArrayProperty(multiDimensionalArrayProperty);
                return multiDimensionalArrayProperty;
            }

            var singleDimensionalArrayProperty = property as SingleDimensionalArrayProperty;
            if (singleDimensionalArrayProperty != null)
            {
                parseSingleDimensionalArrayProperty(singleDimensionalArrayProperty);
                return singleDimensionalArrayProperty;
            }

            var dictionaryProperty = property as DictionaryProperty;
            if (dictionaryProperty != null)
            {
                parseDictionaryProperty(dictionaryProperty);
                return dictionaryProperty;
            }

            var collectionProperty = property as CollectionProperty;
            if (collectionProperty != null)
            {
                parseCollectionProperty(collectionProperty);
                return collectionProperty;
            }

            var complexProperty = property as ComplexProperty;
            if (complexProperty != null)
            {
                parseComplexProperty(complexProperty);
                return complexProperty;
            }

            return property;
        }