Exemplo n.º 1
0
        protected DictionarySerializer(EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            // ReSharper disable DoNotCallOverridableMethodsInConstructor

            _encryptAttribute =
                encryptAttribute
                ?? (EncryptAttribute)Attribute.GetCustomAttribute(KeyType, typeof(EncryptAttribute))
                ?? (EncryptAttribute)Attribute.GetCustomAttribute(ValueType, typeof(EncryptAttribute));

            _options = options;
            _keySerializer = XmlSerializerFactory.Instance.GetSerializer(KeyType, null, _options.WithRootElementName("Key").WithRedactAttribute(null));
            _valueSerializer = XmlSerializerFactory.Instance.GetSerializer(ValueType, null, _options.WithRootElementName("Value"));

            if (DictionaryType.IsReadOnlyDictionary())
            {
                _createDictionary = DefaultDictionaryType.CreateDefaultConstructorFunc<object>();
                _finalizeDictionary = FinalizeCollectionIntoReadOnlyDictionary;
            }
            else if (DictionaryType.IsInterface || DictionaryType.IsAbstract)
            {
                if (DictionaryType.IsAssignableFrom(DefaultDictionaryType))
                {
                    _createDictionary = DefaultDictionaryType.CreateDefaultConstructorFunc<object>();
                }
                else
                {
                    var dictionaryInheritorType =
                        _options.ExtraTypes.FirstOrDefault(t =>
                            !t.IsInterface
                            && !t.IsAbstract
                            && DictionaryType.IsAssignableFrom(t)
                            && t.HasDefaultConstructor());
                    _createDictionary = dictionaryInheritorType.CreateDefaultConstructorFunc<object>();
                }
            }
            else if (DictionaryType.HasDefaultConstructor())
            {
                _createDictionary = DictionaryType.CreateDefaultConstructorFunc<object>();
            }
            else
            {
                throw new ArgumentException("Unable to find suitable dictionary to create.");
            }
            // ReSharper restore DoNotCallOverridableMethodsInConstructor
        }
Exemplo n.º 2
0
        public static IXmlSerializerInternal GetSerializer(Type type, EncryptAttribute encryptAttribute, IXmlSerializerOptions options, string itemElementName)
        {
            return _serializerCache.GetOrAdd(
                XmlSerializerFactory.Instance.CreateKey(type, encryptAttribute, options.WithRootElementName(options.RootElementName + "<>" + itemElementName)),
                _ =>
                {
                    if (type.IsAssignableToGenericIEnumerable())
                    {
                        var itemType = type.GetGenericIEnumerableType().GetGenericArguments()[0];
                        return (IXmlSerializerInternal)Activator.CreateInstance(typeof(ListSerializer<,>).MakeGenericType(type, itemType), encryptAttribute, options, itemElementName);
                    }
                        
                    if (type.IsAssignableToNonGenericIEnumerable())
                    {
                        return (IXmlSerializerInternal)Activator.CreateInstance(typeof(ListSerializer<>).MakeGenericType(type), encryptAttribute, options, itemElementName);
                    }

                    throw new InvalidOperationException(string.Format("Cannot create a ListSerializer of type '{0}'.", type.FullName));
                });
        }
Exemplo n.º 3
0
        public static IXmlSerializerInternal GetSerializer(Type type, EncryptAttribute encryptAttribute, IXmlSerializerOptions options, string itemElementName)
        {
            return(_serializerCache.GetOrAdd(
                       Tuple.Create(type, encryptAttribute, options.WithRootElementName(options.RootElementName + "<>" + itemElementName)),
                       _ =>
            {
                if (type.IsAssignableToGenericIEnumerable())
                {
                    var itemType = type.GetGenericIEnumerableType().GetGenericArguments()[0];
                    return (IXmlSerializerInternal)Activator.CreateInstance(typeof(ListSerializer <,>).MakeGenericType(type, itemType), encryptAttribute, options, itemElementName);
                }

                if (type.IsAssignableToNonGenericIEnumerable())
                {
                    return (IXmlSerializerInternal)Activator.CreateInstance(typeof(ListSerializer <>).MakeGenericType(type), encryptAttribute, options, itemElementName);
                }

                throw new InvalidOperationException(string.Format("Cannot create a ListSerializer of type '{0}'.", type.FullName));
            }));
        }
Exemplo n.º 4
0
        // ReSharper disable DoNotCallOverridableMethodsInConstructor
        protected ListSerializer(EncryptAttribute encryptAttribute, IXmlSerializerOptions options, string itemElementName)
        {
            _encryptAttribute = encryptAttribute ?? ItemType.GetCustomAttribute<EncryptAttribute>();
            _options = options;
            _itemElementName = string.IsNullOrEmpty(itemElementName) ? DefaultItemElementName : itemElementName;
            _itemSerializer = XmlSerializerFactory.Instance.GetSerializer(ItemType, null, _options.WithRootElementName(_itemElementName).AlwaysEmitNil());

            if (CollectionType.IsArray)
            {
                _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc<object>();
            }
            else if (CollectionType.IsReadOnlyCollection())
            {
                _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc<object>();
            }
            else if (CollectionType.IsInterface || CollectionType.IsAbstract)
            {
                if (CollectionType.IsAssignableFrom(DefaultCollectionType))
                {
                    _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc<object>();
                }
                else
                {
                    var collectionInheritorType =
                        _options.ExtraTypes.FirstOrDefault(t =>
                            !t.IsInterface
                            && !t.IsAbstract
                            && CollectionType.IsAssignableFrom(t)
                            && t.HasDefaultConstructor());
                    _createCollection = collectionInheritorType.CreateDefaultConstructorFunc<object>();
                }
            }
            else if (CollectionType.HasDefaultConstructor())
            {
                _createCollection = CollectionType.CreateDefaultConstructorFunc<object>();
            }
            else
            {
                throw new ArgumentException("Unable to find suitable collection to create.");
            }
        }
Exemplo n.º 5
0
        private Func <IXmlSerializerInternal> GetCreateSerializerFunc(PropertyInfo propertyInfo, IXmlSerializerOptions options)
        {
            var redactAttribute = (RedactAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(RedactAttribute));

            if (redactAttribute == null)
            {
                redactAttribute = options.RedactAttribute;
            }

            var attributeAttribute = (XmlAttributeAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(XmlAttributeAttribute));

            if (attributeAttribute != null)
            {
                var attributeName = !string.IsNullOrWhiteSpace(attributeAttribute.AttributeName) ? attributeAttribute.AttributeName : propertyInfo.Name;
                NodeType = NodeType.Attribute;
                Name     = attributeName;
                return(() => new XmlAttributeSerializer(propertyInfo.PropertyType, attributeName, redactAttribute, _encryptAttribute, options));
            }

            var textAttribute = (XmlTextAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(XmlTextAttribute));

            if (textAttribute != null)
            {
                NodeType = NodeType.Text;
                Name     = propertyInfo.Name;
                return(() => new XmlTextSerializer(propertyInfo.PropertyType, redactAttribute, _encryptAttribute, options.ExtraTypes));
            }

            if (redactAttribute != null)
            {
                options = options.WithRedactAttribute(redactAttribute);
            }

            var    elementAttribute = (XmlElementAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(XmlElementAttribute), false);
            string rootElementName;

            if (IsListProperty(propertyInfo))
            {
                var arrayAttribute     = (XmlArrayAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(XmlArrayAttribute), false);
                var arrayItemAttribute = (XmlArrayItemAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(XmlArrayItemAttribute), false);

                if (elementAttribute != null && (arrayAttribute != null || arrayItemAttribute != null))
                {
                    throw new InvalidOperationException("On list types, XmlElementAttribute may not be present with either XmlArrayAttribute or XmlArrayItemAttribute are present.");
                }

                string itemElementName;

                if (elementAttribute != null)
                {
                    rootElementName = null;
                    itemElementName = GetElementName(elementAttribute, x => x.ElementName, propertyInfo.Name);

                    Name = itemElementName;

                    _readsPastLastElement = () => true;
                }
                else
                {
                    rootElementName = GetElementName(arrayAttribute, x => x.ElementName, propertyInfo.Name);

                    var itemElementNameFallback =
                        propertyInfo.PropertyType.IsAssignableToGenericIEnumerable()
                            ? propertyInfo.PropertyType.GetGenericIEnumerableType().GetGenericArguments()[0].GetElementName()
                            : "Item";

                    itemElementName = GetElementName(arrayItemAttribute, x => x.ElementName, itemElementNameFallback);

                    Name = rootElementName;
                }

                NodeType = NodeType.Element;
                return(() => ListSerializer.GetSerializer(propertyInfo.PropertyType, _encryptAttribute, options.WithRootElementName(rootElementName), itemElementName));
            }

            rootElementName = GetElementName(elementAttribute, x => x.ElementName, propertyInfo.Name);

            NodeType = NodeType.Element;
            Name     = rootElementName;
            return(() => XmlSerializerFactory.Instance.GetSerializer(propertyInfo.PropertyType, _encryptAttribute, options.WithRootElementName(rootElementName)));
        }
Exemplo n.º 6
0
        protected DictionarySerializer(EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            // ReSharper disable DoNotCallOverridableMethodsInConstructor

            _encryptAttribute =
                encryptAttribute
                ?? KeyType.GetCustomAttribute <EncryptAttribute>()
                ?? ValueType.GetCustomAttribute <EncryptAttribute>();

            _options         = options;
            _keySerializer   = XmlSerializerFactory.Instance.GetSerializer(KeyType, null, _options.WithRootElementName("Key").WithRedactAttribute(null));
            _valueSerializer = XmlSerializerFactory.Instance.GetSerializer(ValueType, null, _options.WithRootElementName("Value"));

            if (DictionaryType.IsReadOnlyDictionary())
            {
                _createDictionary   = DefaultDictionaryType.CreateDefaultConstructorFunc <object>();
                _finalizeDictionary = FinalizeCollectionIntoReadOnlyDictionary;
            }
            else if (DictionaryType.IsInterface || DictionaryType.IsAbstract)
            {
                if (DictionaryType.IsAssignableFrom(DefaultDictionaryType))
                {
                    _createDictionary = DefaultDictionaryType.CreateDefaultConstructorFunc <object>();
                }
                else
                {
                    var dictionaryInheritorType =
                        _options.ExtraTypes.FirstOrDefault(t =>
                                                           !t.IsInterface &&
                                                           !t.IsAbstract &&
                                                           DictionaryType.IsAssignableFrom(t) &&
                                                           t.HasDefaultConstructor());
                    _createDictionary = dictionaryInheritorType.CreateDefaultConstructorFunc <object>();
                }
            }
            else if (DictionaryType.HasDefaultConstructor())
            {
                _createDictionary = DictionaryType.CreateDefaultConstructorFunc <object>();
            }
            else
            {
                throw new ArgumentException("Unable to find suitable dictionary to create.");
            }
            // ReSharper restore DoNotCallOverridableMethodsInConstructor
        }
Exemplo n.º 7
0
        protected ListSerializer(EncryptAttribute encryptAttribute, IXmlSerializerOptions options, string itemElementName)                                                             // ReSharper disable DoNotCallOverridableMethodsInConstructor
        {
            _encryptAttribute = encryptAttribute ?? (EncryptAttribute)Attribute.GetCustomAttribute(ItemType, typeof(EncryptAttribute));
            _options          = options;
            _itemElementName  = string.IsNullOrEmpty(itemElementName) ? DefaultItemElementName : itemElementName;
            _itemSerializer   = XmlSerializerFactory.Instance.GetSerializer(ItemType, null, _options.WithRootElementName(_itemElementName).AlwaysEmitNil());

            if (CollectionType.IsArray)
            {
                _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc <object>();
            }
            else if (CollectionType.IsReadOnlyCollection())
            {
                _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc <object>();
            }
            else if (CollectionType.IsInterface || CollectionType.IsAbstract)
            {
                if (CollectionType.IsAssignableFrom(DefaultCollectionType))
                {
                    _createCollection = DefaultCollectionType.CreateDefaultConstructorFunc <object>();
                }
                else
                {
                    var collectionInheritorType =
                        _options.ExtraTypes.FirstOrDefault(t =>
                                                           !t.IsInterface &&
                                                           !t.IsAbstract &&
                                                           CollectionType.IsAssignableFrom(t) &&
                                                           t.HasDefaultConstructor());
                    _createCollection = collectionInheritorType.CreateDefaultConstructorFunc <object>();
                }
            }
            else if (CollectionType.HasDefaultConstructor())
            {
                _createCollection = CollectionType.CreateDefaultConstructorFunc <object>();
            }
            else
            {
                throw new ArgumentException("Unable to find suitable collection to create.");
            }
        }                                                                                                                                           // ReSharper restore DoNotCallOverridableMethodsInConstructor