internal int CreateKey(Type type, EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            unchecked
            {
                var key = type.GetHashCode();

                key = (key * 397) ^ (string.IsNullOrWhiteSpace(options.DefaultNamespace) ? "" : options.DefaultNamespace).GetHashCode();

                if (options.ExtraTypes != null)
                {
                    key = options.ExtraTypes
                          .Where(extraType => extraType != null)
                          .Distinct(EqualityComparer <Type> .Default)
                          .OrderBy(extraType => extraType.FullName)
                          .Aggregate(key, (current, extraType) => (current * 397) ^ extraType.GetHashCode());
                }

                key = (key * 397) ^ (string.IsNullOrWhiteSpace(options.RootElementName) ? type.Name : options.RootElementName).GetHashCode();

                if (options.RedactAttribute != null)
                {
                    key = (key * 397) ^ options.RedactAttribute.GetHashCode();
                }

                key = (key * 397) ^ (encryptAttribute != null).GetHashCode();

                key = (key * 397) ^ options.TreatEmptyElementAsString.GetHashCode();

                return(key);
            }
        }
        public XmlAttributeSerializer(Type type, string attributeName, RedactAttribute redactAttribute, EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            _attributeName = attributeName;
            _encryptAttribute = encryptAttribute;

            if (!ValueTypes.TryGetValueConverter(type, redactAttribute, options.ExtraTypes, out _valueConverter))
            {
                _valueConverter = SimpleTypeValueConverter.Create(type, redactAttribute);
            }
        }
Beispiel #3
0
 public static IXmlSerializerOptions WithRootElementName(this IXmlSerializerOptions options, string rootElementName)
 {
     return(new XmlSerializerOptions
     {
         DefaultNamespace = options.DefaultNamespace,
         ExtraTypes = options.ExtraTypes,
         RootElementName = rootElementName,
         RedactAttribute = options.RedactAttribute,
         TreatEmptyElementAsString = options.TreatEmptyElementAsString,
         ShouldAlwaysEmitNil = options.ShouldAlwaysEmitNil
     });
 }
Beispiel #4
0
 public static IXmlSerializerOptions WithAdditionalExtraTypes(this IXmlSerializerOptions options, IEnumerable <Type> additionalExtraTypes)
 {
     return(new XmlSerializerOptions
     {
         DefaultNamespace = options.DefaultNamespace,
         ExtraTypes = (options.ExtraTypes ?? new Type[0]).Concat(additionalExtraTypes).Distinct().ToArray(),
         RootElementName = options.RootElementName,
         RedactAttribute = options.RedactAttribute,
         TreatEmptyElementAsString = options.TreatEmptyElementAsString,
         ShouldAlwaysEmitNil = options.ShouldAlwaysEmitNil
     });
 }
Beispiel #5
0
 public static IXmlSerializerOptions AlwaysEmitNil(this IXmlSerializerOptions options)
 {
     return(new XmlSerializerOptions
     {
         DefaultNamespace = options.DefaultNamespace,
         ExtraTypes = options.ExtraTypes,
         RootElementName = options.RootElementName,
         RedactAttribute = options.RedactAttribute,
         TreatEmptyElementAsString = options.TreatEmptyElementAsString,
         ShouldAlwaysEmitNil = true,
         ShouldUseAttributeDefinedInInterface = options.ShouldUseAttributeDefinedInInterface
     });
 }
Beispiel #6
0
        public XmlElementSerializer(EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            if (!typeof(T).IsPrimitiveLike() && !typeof(T).IsNullablePrimitiveLike() && !ValueTypes.IsRegistered(typeof(T)))
            {
                throw new InvalidOperationException("Generic argument of XmlElementSerializer<T> must be an primitive, like a primitive (e.g. Guid, DateTime), a nullable of either, or one of: Enum, Type, or Uri.");
            }

            _elementName      = options.RootElementName;
            _alwaysEmitNil    = options.ShouldAlwaysEmitNil;
            _encryptAttribute = encryptAttribute;

            if (!ValueTypes.TryGetValueConverter(typeof(T), options.RedactAttribute, options.ExtraTypes, out _valueConverter))
            {
                _valueConverter = SimpleTypeValueConverter.Create(typeof(T), options.RedactAttribute);
            }
        }
 public static IXmlSerializerInternal GetSerializer(Type type, EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
 {
     return _serializerCache.GetOrAdd(
         XmlSerializerFactory.Instance.CreateKey(type, encryptAttribute, options),
         _ =>
         {
             try
             {
                 return (IXmlSerializerInternal)Activator.CreateInstance(typeof(CustomSerializer<>).MakeGenericType(type), encryptAttribute, options);
             }
             catch (TargetInvocationException ex) // True exception gets masked due to reflection. Preserve stacktrace and rethrow
             {
                 PreserveStackTrace(ex.InnerException);
                 throw ex.InnerException;
             }
         });
 }
        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
        }
Beispiel #9
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
        }
Beispiel #10
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));
                });
        }
Beispiel #11
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
        // 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.");
            }
        }
Beispiel #13
0
 public DynamicSerializer(EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
 {
     _encryptAttribute = encryptAttribute;
     _options          = options;
 }
Beispiel #14
0
        public static IXmlSerializerInternal GetSerializer <T>(EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            var serializer = new DynamicSerializer(encryptAttribute, options);

            if (typeof(T) == typeof(object))
            {
                return(serializer);
            }

            if (typeof(T) == typeof(ExpandoObject))
            {
                return(new DynamicSerializerExpandoObjectProxy(serializer));
            }

            throw new InvalidOperationException("The only valid generic arguments for DynamicSerializer.GetSerializer<T> are object, dynamic, and ExpandoObject");
        }
Beispiel #15
0
        public IXmlSerializerInternal GetSerializer <T>(EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            return(_serializerCache.GetOrAdd(
                       Tuple.Create(typeof(T), encryptAttribute, options),
                       _ =>
            {
                var type = typeof(T);

                if (type == typeof(object) || type == typeof(ExpandoObject))
                {
                    return DynamicSerializer.GetSerializer <T>(encryptAttribute, options);
                }

                IXmlSerializerInternal serializer;

                if (type.IsPrimitiveLike() ||
                    type.IsNullablePrimitiveLike() ||
                    ValueTypes.IsRegistered(type))
                {
                    serializer = new XmlElementSerializer <T>(encryptAttribute, options);
                }
                else if (type.IsAssignableToNonGenericIDictionary() || type.IsAssignableToGenericIDictionary() || type.IsReadOnlyDictionary())
                {
                    serializer = DictionarySerializer.GetSerializer(type, encryptAttribute, options);
                }
                else if (type.IsAssignableToNonGenericIEnumerable() || type.IsAssignableToGenericIEnumerable())
                {
                    serializer = ListSerializer.GetSerializer(type, encryptAttribute, options, null);
                }
                else
                {
                    serializer = CustomSerializer.GetSerializer(type, encryptAttribute, options);
                }

                return serializer;
            }));
        }
Beispiel #16
0
        public IXmlSerializerInternal GetSerializer(Type type, EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            var getSerializer = _getSerializerMap.GetOrAdd(
                type,
                t =>
            {
                var getSerializerMethod =
                    typeof(XmlSerializerFactory)
                    .GetMethod("GetSerializer", new[] { typeof(EncryptAttribute), typeof(IXmlSerializerOptions) })
                    .MakeGenericMethod(type);

                return((Func <EncryptAttribute, IXmlSerializerOptions, IXmlSerializerInternal>)Delegate.CreateDelegate(typeof(Func <EncryptAttribute, IXmlSerializerOptions, IXmlSerializerInternal>), this, getSerializerMethod));
            });

            return(getSerializer(encryptAttribute, options));
        }
        public SerializableProperty(PropertyInfo propertyInfo, IXmlSerializerOptions options)
        {
            _encryptAttribute =
                (EncryptAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(EncryptAttribute))
                ?? (EncryptAttribute)Attribute.GetCustomAttribute(propertyInfo.PropertyType, typeof(EncryptAttribute));
            _isListDecoratedWithXmlElement =
                typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType) &&
                !(propertyInfo.PropertyType == typeof(string)) &&
                Attribute.GetCustomAttributes(propertyInfo, typeof(XmlElementAttribute)).Any();

            _getValueFunc = DynamicMethodFactory.CreateFunc <object>(propertyInfo.GetGetMethod());

            if (!propertyInfo.DeclaringType.IsAnonymous() &&
                !(propertyInfo.IsReadOnlyProperty() && propertyInfo.PropertyType.IsGenericIEnumerable()))
            {
                if (propertyInfo.IsSerializableReadOnlyProperty())
                {
                    _setValueFunc = GetSerializableReadonlyPropertySetValueFunc(propertyInfo);
                }
                else
                {
                    if (IsListProperty(propertyInfo))
                    {
                        var setValue  = DynamicMethodFactory.CreateAction(propertyInfo.GetSetMethod());
                        var addValues = GetAddEnumerableValuesAction(propertyInfo.PropertyType);

                        var targetType = propertyInfo.PropertyType;

                        _setValueFunc = (destinationInstance, sourceCollection) =>
                        {
                            sourceCollection = sourceCollection.ConvertIfNecessary(targetType);

                            if (_getValueFunc(destinationInstance) == null)
                            {
                                setValue(destinationInstance, sourceCollection);
                            }
                            else
                            {
                                addValues(destinationInstance, sourceCollection);
                            }
                        };
                    }
                    else
                    {
                        var setMethod = propertyInfo.GetSetMethod();
                        if (setMethod != null)
                        {
                            _setValueFunc = DynamicMethodFactory.CreateAction(setMethod);
                        }
                        else
                        {
                            _setValueFunc = null;
                        }
                    }
                }
            }

            _shouldSerializeFunc  = GetShouldSerializeFunc(propertyInfo);
            _readsPastLastElement = () => false;
            _serializer           = new Lazy <IXmlSerializerInternal>(GetCreateSerializerFunc(propertyInfo, options));
        }
        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)));
        }
Beispiel #19
0
        public static IXmlSerializerInternal GetSerializer(Type type, EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            return(_serializerCache.GetOrAdd(
                       Tuple.Create(type, encryptAttribute, options),
                       _ =>
            {
                Func <Type, IXmlSerializerInternal> createDictionarySerializer =
                    genericDictionaryInterface =>
                {
                    var genericArguments = genericDictionaryInterface.GetGenericArguments();
                    var keyType = genericArguments[0];
                    var valueType = genericArguments[1];
                    return (IXmlSerializerInternal)Activator.CreateInstance(typeof(DictionarySerializer <, ,>).MakeGenericType(type, keyType, valueType), encryptAttribute, options);
                };

                if (type.IsAssignableToGenericIDictionary())
                {
                    return createDictionarySerializer(type.GetGenericIDictionaryType());
                }

                if (type.IsAssignableToNonGenericIDictionary())
                {
                    return (IXmlSerializerInternal)Activator.CreateInstance(typeof(DictionarySerializer <>).MakeGenericType(type), encryptAttribute, options);
                }

                if (type.IsReadOnlyDictionary())
                {
                    return createDictionarySerializer(type.GetIReadOnlyDictionaryInterface());
                }

                throw new InvalidOperationException(string.Format("Cannot create a DictionarySerializer of type '{0}'.", type.FullName));
            }));
        }
        public static IXmlSerializerInternal GetSerializer(Type type, EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            return _serializerCache.GetOrAdd(
                XmlSerializerFactory.Instance.CreateKey(type, encryptAttribute, options),
                _ =>
                {
                    Func<Type, IXmlSerializerInternal> createDictionarySerializer =
                        genericDictionaryInterface =>
                        {
                            var genericArguments = genericDictionaryInterface.GetGenericArguments();
                            var keyType = genericArguments[0];
                            var valueType = genericArguments[1];
                            return (IXmlSerializerInternal)Activator.CreateInstance(typeof(DictionarySerializer<,,>).MakeGenericType(type, keyType, valueType), encryptAttribute, options);
                        };

                    if (type.IsAssignableToGenericIDictionary())
                    {
                        return createDictionarySerializer(type.GetGenericIDictionaryType());
                    }

                    if (type.IsAssignableToNonGenericIDictionary())
                    {
                        return (IXmlSerializerInternal)Activator.CreateInstance(typeof(DictionarySerializer<>).MakeGenericType(type), encryptAttribute, options);
                    }

                    if (type.IsReadOnlyDictionary())
                    {
                        return createDictionarySerializer(type.GetIReadOnlyDictionaryInterface());
                    }

                    throw new InvalidOperationException(string.Format("Cannot create a DictionarySerializer of type '{0}'.", type.FullName));
                });
        }
Beispiel #21
0
 public static TAttribute GetCustomAttribute <TAttribute>(
     this PropertyInfo property, IXmlSerializerOptions options)
     where TAttribute : Attribute
 {
     return(GetCustomAttribute <TAttribute>(property, options.ShouldUseAttributeDefinedInInterface));
 }
Beispiel #22
0
 public static IXmlSerializerOptions WithAdditionalExtraTypes(this IXmlSerializerOptions options, params Type[] additionalExtraTypes)
 {
     return(options.WithAdditionalExtraTypes((IEnumerable <Type>)additionalExtraTypes));
 }
        public XmlAttributeSerializer(Type type, string attributeName, RedactAttribute redactAttribute, EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
        {
            _attributeName    = attributeName;
            _encryptAttribute = encryptAttribute;

            if (!ValueTypes.TryGetValueConverter(type, redactAttribute, options.ExtraTypes, out _valueConverter))
            {
                _valueConverter = SimpleTypeValueConverter.Create(type, redactAttribute);
            }
        }
 public static IXmlSerializerInternal GetSerializer(Type type, EncryptAttribute encryptAttribute, IXmlSerializerOptions options)
 {
     return(_serializerCache.GetOrAdd(
                XmlSerializerFactory.Instance.CreateKey(type, encryptAttribute, options),
                _ =>
     {
         try
         {
             return (IXmlSerializerInternal)Activator.CreateInstance(typeof(CustomSerializer <>).MakeGenericType(type), encryptAttribute, options);
         }
         catch (TargetInvocationException ex)     // True exception gets masked due to reflection. Preserve stacktrace and rethrow
         {
             PreserveStackTrace(ex.InnerException);
             throw ex.InnerException;
         }
     }));
 }
Beispiel #25
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));
            }));
        }