コード例 #1
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;
            }));
        }
コード例 #2
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);
            }
        }