Stores pre calculated information about a member or type that is used when serializing that member.
Inheritance: IGetterSetter
		public ListTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
			: base(memberInfo, memberInfo.ReturnType, cache, options)
		{
			typeToItemMap = new Dictionary<Type, ListItem>();
			aliasToItemMap = new Dictionary<string, ListItem>();

			var attribute = (XmlListElementDynamicTypeProviderAttribute)memberInfo.GetFirstApplicableAttribute(typeof(XmlListElementDynamicTypeProviderAttribute));

			if (attribute != null)
			{
				if (dynamicTypeResolver == null)
				{
					try
					{
						dynamicTypeResolver  = (IXmlListElementDynamicTypeProvider)Activator.CreateInstance(attribute.ProviderType, new object[0]);
					}
					catch (Exception)
					{
					}
				}

				if (dynamicTypeResolver == null)
				{
					dynamicTypeResolver = (IXmlListElementDynamicTypeProvider)Activator.CreateInstance(attribute.ProviderType, new object[] {memberInfo, cache, options});
				}
			}

			serializationMemberInfo = memberInfo;

			this.cache = cache;

			Scan(memberInfo, cache, options);
		}
Example #2
0
        /// <summary>
        /// Checks if see if an object should be serialized.
        /// </summary>
        /// <remarks>
        /// <p>
        /// An object shouldn't be serialized if it has already been serialized.
        /// This method automatically checks if the object has been serialized
        /// by examining the serialization stack.  This stack is maintained by
        /// the SerializationStart and SerializationEnd methods.
        /// </p>
        /// <p>
        /// You should call SerializationStart and SerializationEnd when you start
        /// and finish serializing an object.
        /// </p>
        /// </remarks>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool ShouldSerialize(object obj, SerializationMemberInfo memberInfo)
        {
            IXmlSerializationShouldSerializeProvider shouldSerialize;

            if (obj == null && !memberInfo.SerializeIfNull)
            {
                return(false);
            }

            if ((shouldSerialize = obj as IXmlSerializationShouldSerializeProvider) != null)
            {
                if (!shouldSerialize.ShouldSerialize(this.SerializerOptions, this.Parameters))
                {
                    return(false);
                }
            }

            for (var i = 0; i < stack.Count; i++)
            {
                if (stack[i] == obj)
                {
                    return(false);
                }
            }

            return(true);
        }
        public ListTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            m_TypeToItemMap = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
            m_AliasToItemMap = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);

            Scan(memberInfo, cache, options);
        }
Example #4
0
        public ListTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            m_TypeToItemMap  = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
            m_AliasToItemMap = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);

            Scan(memberInfo, cache, options);
        }
        public ListTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
            : base(memberInfo, memberInfo.ReturnType, cache, options)
        {
            typeToItemMap = new Dictionary<Type, ListItem>();
            aliasToItemMap = new Dictionary<string, ListItem>();

            var attribute = (XmlListElementDynamicTypeProviderAttribute)memberInfo.GetFirstApplicableAttribute(typeof(XmlListElementDynamicTypeProviderAttribute));

            if (attribute != null)
            {
                if (dynamicTypeResolver == null)
                {
                    try
                    {
                        dynamicTypeResolver  = (IXmlListElementDynamicTypeProvider)Activator.CreateInstance(attribute.ProviderType, new object[0]);
                    }
                    catch (Exception)
                    {
                    }
                }

                if (dynamicTypeResolver == null)
                {
                    dynamicTypeResolver = (IXmlListElementDynamicTypeProvider)Activator.CreateInstance(attribute.ProviderType, new object[] {memberInfo, cache, options});
                }
            }

            serializationMemberInfo = memberInfo;

            this.cache = cache;

            Scan(memberInfo, cache, options);
        }
Example #6
0
        public TypeSerializer GetTypeSerializerBySerializerType(Type type, SerializationMemberInfo memberInfo)
        {
            var serializer = typeSerializerFactory.NewTypeSerializerBySerializerType(type, memberInfo, this);

            Add(serializer, memberInfo);

            return(serializer);
        }
        public DictionaryTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
            : base(memberInfo, memberInfo.ReturnType, cache, options)
        {
            typeToItemMap  = new Dictionary <Type, DictionaryItem>();
            aliasToItemMap = new Dictionary <string, DictionaryItem>();

            Scan(memberInfo, cache, options);
        }
        public DictionaryTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
            : base(memberInfo, memberInfo.ReturnType, cache, options)
        {
            typeToItemMap = new Dictionary<Type, DictionaryItem>();
            aliasToItemMap = new Dictionary<string, DictionaryItem>();

            Scan(memberInfo, cache, options);
        }
Example #9
0
        public EnumTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            m_Type = memberInfo.LogicalType;

            if (!typeof(Enum).IsAssignableFrom(m_Type))
            {
                throw new ArgumentException(this.GetType().Name + " only works with Enum types");
            }
        }
        public EnumTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            supportedType = memberInfo.ReturnType;

            if (!typeof(Enum).IsAssignableFrom(supportedType))
            {
                throw new ArgumentException(this.GetType().Name + " only works with Enum types");
            }
        }
        public StringableTypeSerializer(Type type, SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            supportedType = type;
            if (memberInfo != null)
            {
                formatAttribute= (XmlFormatAttribute) memberInfo.GetFirstApplicableAttribute(typeof (XmlFormatAttribute));
            }

            formatSpecified = formatAttribute != null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="memberInfo"></param>
        /// <param name="cache"></param>
        /// <param name="options"></param>
        public DateTimeTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            m_FormatAttribute = (XmlDateTimeFormatAttribute)memberInfo.GetFirstApplicableAttribute(typeof(XmlDateTimeFormatAttribute));

            if (m_FormatAttribute == null)
            {
                m_FormatAttribute = new XmlDateTimeFormatAttribute("G");
                m_FormatSpecified = false;
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="memberInfo"></param>
        /// <param name="cache"></param>
        /// <param name="options"></param>
        public DateTimeTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            m_FormatAttribute = (XmlDateTimeFormatAttribute)memberInfo.GetFirstApplicableAttribute(typeof(XmlDateTimeFormatAttribute));

            if (m_FormatAttribute == null)
            {
                m_FormatAttribute = new XmlDateTimeFormatAttribute("G");
                m_FormatSpecified = false;
            }
        }
Example #14
0
        public StringableTypeSerializer(Type type, SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            supportedType = type;
            if (memberInfo != null)
            {
                formatAttribute = (XmlFormatAttribute)memberInfo.GetFirstApplicableAttribute(typeof(XmlFormatAttribute));
            }

            formatSpecified = formatAttribute != null;
        }
Example #15
0
 public LightSerializationMember(SerializationMemberInfo memberInfo)
 {
     this.Getter                = memberInfo.Getter;
     this.Setter                = memberInfo.Setter;
     this.Serializer            = memberInfo.Serializer;
     this.MemberInfo            = memberInfo.MemberInfo;
     this.SerializedName        = memberInfo.GetSerializedName();
     this.SerializeAsCData      = memberInfo.SerializeAsCData;
     this.XmlTreatAsNullIfEmpty = memberInfo.HasApplicableAttribute(typeof(XmlTreatAsNullIfEmptyAttribute));
     this.LogicalType           = memberInfo.LogicalType;
 }
Example #16
0
 public virtual void Add(TypeSerializer serializer, SerializationMemberInfo memberInfo)
 {
     if (serializer.MemberBound)
     {
         Add(serializer, (object)memberInfo);
     }
     else
     {
         Add(serializer);
     }
 }
Example #17
0
        /// <summary>
        /// Constructs a new XmlSerializer supporting type <see cref="type"/> with the provided options
        /// </summary>
        /// <param name="options">The options</param>
        internal XmlSerializer(Type type, SerializerOptions options)
        {
            this.Options = options;

            var factory = new StandardTypeSerializerFactory(options);

            var cache = new TypeSerializerCache(factory);

            rootMemberInfo = new SerializationMemberInfo(type, options, cache);

            if (!rootMemberInfo.Serializable)
            {
                throw new XmlSerializerException("Unable to serialize given type, type must have [XmlElement] attribute.");
            }
        }
Example #18
0
        public ComplexTypeTypeSerializer(SerializationMemberInfo memberInfo, Type type, TypeSerializerCache cache, SerializerOptions options)
        {
            supportedType = type;

            serializationMemberInfo = memberInfo;

            elementMembersMap   = new ListDictionary();
            attributeMembersMap = new ListDictionary();

            if (memberInfo != null && memberInfo.IncludeIfUnattributed)
            {
                memberBound = true;
            }

            cache.Add(this, memberInfo);

            Scan(cache, options);
        }
        public ComplexTypeTypeSerializer(SerializationMemberInfo memberInfo, Type type, TypeSerializerCache cache, SerializerOptions options)
        {
            supportedType = type;

            serializationMemberInfo = memberInfo;

            elementMembersMap = new ListDictionary();
            attributeMembersMap = new ListDictionary();

            if (memberInfo != null && memberInfo.IncludeIfUnattributed)
            {
                memberBound = true;
            }

            cache.Add(this, memberInfo);

            Scan(cache, options);
        }
Example #20
0
        public TypeSerializer GetTypeSerializerBySupportedType(Type type, SerializationMemberInfo memberInfo)
        {
            TypeSerializer serializer;

            if (cache.TryGetValue(new Pair <Type, object>(type, null), out serializer))
            {
                return(serializer);
            }

            if (cache.TryGetValue(new Pair <Type, object>(type, memberInfo), out serializer))
            {
                return(serializer);
            }

            serializer = typeSerializerFactory.NewTypeSerializerBySupportedType(type, memberInfo, this);

            Add(serializer, memberInfo);

            return(serializer);
        }
Example #21
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="memberInfo"></param>
        /// <param name="cache"></param>
        /// <param name="options"></param>
        private void AddMember(MemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            SerializationMemberInfo serializationMemberInfo;

            serializationMemberInfo = new SerializationMemberInfo(memberInfo, options, cache);

            if (serializationMemberInfo.SerializedNodeType == XmlNodeType.Element)
            {
                m_ElementMembersMap[serializationMemberInfo.GetSerializedName()] = new LightSerializationMember(serializationMemberInfo);

                return;
            }
            else if (serializationMemberInfo.SerializedNodeType == XmlNodeType.Attribute)
            {
                if (!(serializationMemberInfo.Serializer is TypeSerializerWithSimpleTextSupport))
                {
                    throw new InvalidOperationException("Serializer for member doesn't support serializing to an attribute.");
                }

                m_AttributeMembersMap[serializationMemberInfo.GetSerializedName()] = new LightSerializationMember(serializationMemberInfo);
            }
        }
        protected virtual void AddMember(MemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            SerializationMemberInfo localSerializationMemberInfo;

            if (this.MemberBound && this.serializationMemberInfo.IncludeIfUnattributed)
            {
                bool ok;
                var fieldInfo = memberInfo as FieldInfo;
                var propertyInfo = memberInfo as PropertyInfo;

                if (fieldInfo != null && fieldInfo.IsPublic)
                {
                    ok = true;
                }
                else if (propertyInfo != null
                    && propertyInfo.CanRead && propertyInfo.CanWrite
                    && propertyInfo.GetSetMethod().IsPublic
                    && propertyInfo.GetGetMethod().IsPublic)
                {
                    ok = true;
                }
                else
                {
                    ok = false;
                }

                if (ok)
                {
                    localSerializationMemberInfo = new SerializationMemberInfo(memberInfo, options, cache, true);

                    elementMembersMap[localSerializationMemberInfo.SerializedName] = localSerializationMemberInfo;

                    return;
                }
            }

            localSerializationMemberInfo = new SerializationMemberInfo(memberInfo, options, cache);

            if (localSerializationMemberInfo.SerializedNodeType == XmlNodeType.Element)
            {
                if (localSerializationMemberInfo.Namespace.Length > 0)
                {
                    elementMembersMap[localSerializationMemberInfo.Namespace + (char)0xff + localSerializationMemberInfo.SerializedName] = localSerializationMemberInfo;
                }
                else
                {
                    elementMembersMap[localSerializationMemberInfo.SerializedName] = localSerializationMemberInfo;
                }

                return;
            }
            else if (localSerializationMemberInfo.SerializedNodeType == XmlNodeType.Attribute)
            {
                if (localSerializationMemberInfo.Namespace.Length > 0)
                {
                    attributeMembersMap[localSerializationMemberInfo.Namespace + (char)0xff + localSerializationMemberInfo.SerializedName] = localSerializationMemberInfo;
                }
                else
                {
                    attributeMembersMap[localSerializationMemberInfo.SerializedName] = localSerializationMemberInfo;
                }
            }
            else if (localSerializationMemberInfo.SerializedNodeType == XmlNodeType.Text)
            {
                if (TextMember != null  && !TextMember.Equals(localSerializationMemberInfo))
                    throw new Exception($"There should only be one XmlTextAttribute in type {((Type)this.serializationMemberInfo.MemberInfo).FullName}");
                TextMember = localSerializationMemberInfo;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="memberInfo"></param>
        /// <param name="cache"></param>
        /// <param name="options"></param>
        private void Scan(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            IList attributes;
            SerializationMemberInfo smi;
            XmlSerializationAttribute[] attribs;

            attributes = new ArrayList(10);

            // Get the ElementType attributes specified on the type itself as long
            // as we're not the type itself!

            if (memberInfo.MemberInfo != memberInfo.LogicalType)
            {
                smi = new SerializationMemberInfo(memberInfo.LogicalType, options, cache);

                attribs = smi.GetApplicableAttributes(typeof(XmlListElementAttribute));

                foreach (Attribute a in attribs)
                {
                    attributes.Add(a);
                }
            }

            // Get the ElementType attributes specified on the member.

            attribs = memberInfo.GetApplicableAttributes(typeof(XmlListElementAttribute));

            foreach (Attribute a in attribs)
            {
                attributes.Add(a);
            }

            foreach (XmlListElementAttribute attribute in attributes)
            {
                SerializationMemberInfo smi2;
                ListItem listItem = new ListItem();

                smi2 = new SerializationMemberInfo(attribute.ItemType, options, cache);

                if (attribute.Alias == null)
                {
                    attribute.Alias = smi2.SerializedName;
                }

                listItem.Attribute = attribute;
                listItem.Alias = attribute.Alias;

                // Check if a specific type of serializer is specified.

                if (attribute.SerializerType == null)
                {
                    // Figure out the serializer based on the type of the element.

                    listItem.Serializer = cache.GetTypeSerializerBySupportedType(attribute.ItemType, smi2);
                }
                else
                {
                    // Get the type of serializer they specify.

                    listItem.Serializer = cache.GetTypeSerializerBySerializerType(attribute.SerializerType, smi2);
                }

                m_TypeToItemMap[attribute.ItemType] = listItem;
                m_AliasToItemMap[attribute.Alias] = listItem;
            }

            if (m_TypeToItemMap.Count == 0)
            {
                if (memberInfo.LogicalType.IsArray)
                {
                    ListItem listItem;
                    Type elementType;

                    listItem = new ListItem();

                    elementType = memberInfo.LogicalType.GetElementType();
                    listItem.Alias = elementType.Name;

                    listItem.Serializer = cache.GetTypeSerializerBySupportedType(elementType, new SerializationMemberInfo(elementType, options, cache));

                    m_TypeToItemMap[elementType] = listItem;
                    m_AliasToItemMap[listItem.Alias] = listItem;
                }
            }

            if (m_TypeToItemMap.Count == 0)
            {
                throw new InvalidOperationException("Must specify at least one XmlListElementype.");
            }

            m_ListType = memberInfo.LogicalType;

            if (m_ListType.IsAbstract)
            {
                m_ListType = typeof(ArrayList);
            }
        }
        private void Scan(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            XmlSerializationAttribute[] attribs;

            var attributes = new List <Attribute>();

            // Get the ElementType attributes specified on the type itself as long
            // as we're not the type itself!

            if (memberInfo.MemberInfo != memberInfo.ReturnType)
            {
                var smi = new SerializationMemberInfo(memberInfo.ReturnType, options, cache);

                attribs = smi.GetApplicableAttributes(typeof(XmlDictionaryElementTypeAttribute));

                foreach (XmlSerializationAttribute a in attribs)
                {
                    attributes.Add(a);
                }
            }

            // Get the ElementType attributes specified on the member.

            attribs = memberInfo.GetApplicableAttributes(typeof(XmlDictionaryElementTypeAttribute));

            foreach (var a in attribs)
            {
                attributes.Add(a);
            }

            foreach (XmlDictionaryElementTypeAttribute attribute in attributes)
            {
                var dictionaryItem = new DictionaryItem();

                var smi2 = new SerializationMemberInfo(attribute.ElementType, options, cache);

                if (attribute.TypeAlias == null)
                {
                    attribute.TypeAlias = smi2.SerializedName;
                }

                dictionaryItem.attribute = attribute;
                dictionaryItem.typeAlias = attribute.TypeAlias;

                // Check if a specific type of serializer is specified.

                if (attribute.SerializerType == null)
                {
                    // Figure out the serializer based on the type of the element.

                    dictionaryItem.serializer = cache.GetTypeSerializerBySupportedType(attribute.ElementType, smi2);
                }
                else
                {
                    // Get the type of serializer they specify.

                    dictionaryItem.serializer = cache.GetTypeSerializerBySerializerType(attribute.SerializerType, smi2);
                }

                primaryDictionaryItem = dictionaryItem;

                typeToItemMap[attribute.ElementType] = dictionaryItem;
                aliasToItemMap[attribute.TypeAlias]  = dictionaryItem;
            }

            if (aliasToItemMap.Count != 1)
            {
                primaryDictionaryItem = null;
            }
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="memberInfo"></param>
 /// <param name="cache"></param>
 /// <param name="options"></param>
 public DictionaryTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
 {
 }
 public SqlDatabaseContextInfoDynamicTypeProvider(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
 {
 }
Example #27
0
 public ColorSerializer(Type type, SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
     : base(type, memberInfo, cache, options)
 {
 }
        public override TypeSerializer NewTypeSerializerBySerializerType(Type serializerType, SerializationMemberInfo memberInfo, TypeSerializerCache cache)
        {
            TypeSerializer retval = null;

            try
            {
                retval = (TypeSerializer)Activator.CreateInstance(serializerType, new object[] { cache, options });
            }
            catch (Exception)
            {
            }

            if (retval == null && memberInfo != null)
            {
                try
                {
                    retval = (TypeSerializer)Activator.CreateInstance(serializerType, new object[] { memberInfo, cache, options });
                }
                catch (Exception)
                {
                }
            }

            if (retval == null && memberInfo != null)
            {
                try
                {
                    retval = (TypeSerializer)Activator.CreateInstance(serializerType, new object[] { memberInfo.ReturnType });
                }
                catch (Exception)
                {
                }
            }

            if (retval == null)
            {
                try
                {
                    retval = (TypeSerializer)Activator.CreateInstance(serializerType, new object[0]);
                }
                catch (Exception)
                {
                }
            }

            if (retval == null)
            {
                throw new XmlSerializerException("Unable to create TypeSerializer: " + serializerType.GetType().ToString());
            }

            return(retval);
        }
 public void PushCurrentMemberInfo(SerializationMemberInfo memberInfo)
 {
     serializationMemberInfoStack.Push(memberInfo);
 }
        private void Scan(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            XmlSerializationAttribute[] attribs;

            var attributes = new List<Attribute>();

            // Get the ElementType attributes specified on the type itself as long
            // as we're not the type itself!

            if (memberInfo.MemberInfo != memberInfo.ReturnType)
            {
                var smi = new SerializationMemberInfo(memberInfo.ReturnType, options, cache);

                attribs = smi.GetApplicableAttributes(typeof(XmlDictionaryElementTypeAttribute));

                foreach (XmlSerializationAttribute a in attribs)
                {
                    attributes.Add(a);
                }
            }

            // Get the ElementType attributes specified on the member.

            attribs = memberInfo.GetApplicableAttributes(typeof(XmlDictionaryElementTypeAttribute));

            foreach (var a in attribs)
            {
                attributes.Add(a);
            }

            foreach (XmlDictionaryElementTypeAttribute attribute in attributes)
            {
                var dictionaryItem = new DictionaryItem();

                var smi2 = new SerializationMemberInfo(attribute.ElementType, options, cache);

                if (attribute.TypeAlias == null)
                {
                    attribute.TypeAlias = smi2.SerializedName;
                }

                dictionaryItem.attribute = attribute;
                dictionaryItem.typeAlias = attribute.TypeAlias;

                // Check if a specific type of serializer is specified.

                if (attribute.SerializerType == null)
                {
                    // Figure out the serializer based on the type of the element.

                    dictionaryItem.serializer = cache.GetTypeSerializerBySupportedType(attribute.ElementType, smi2);
                }
                else
                {
                    // Get the type of serializer they specify.

                    dictionaryItem.serializer = cache.GetTypeSerializerBySerializerType(attribute.SerializerType, smi2);
                }

                primaryDictionaryItem = dictionaryItem;

                typeToItemMap[attribute.ElementType] = dictionaryItem;
                aliasToItemMap[attribute.TypeAlias] = dictionaryItem;
            }

            if (aliasToItemMap.Count != 1)
            {
                primaryDictionaryItem = null;
            }
        }
Example #31
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="memberInfo"></param>
        /// <param name="cache"></param>
        /// <param name="options"></param>
        private void Scan(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            IList attributes;
            SerializationMemberInfo smi;

            XmlSerializationAttribute[] attribs;

            attributes = new ArrayList(10);

            // Get the ElementType attributes specified on the type itself as long
            // as we're not the type itself!

            if (memberInfo.MemberInfo != memberInfo.LogicalType)
            {
                smi = new SerializationMemberInfo(memberInfo.LogicalType, options, cache);

                attribs = smi.GetApplicableAttributes(typeof(XmlListElementAttribute));

                foreach (Attribute a in attribs)
                {
                    attributes.Add(a);
                }
            }

            // Get the ElementType attributes specified on the member.

            attribs = memberInfo.GetApplicableAttributes(typeof(XmlListElementAttribute));

            foreach (Attribute a in attribs)
            {
                attributes.Add(a);
            }


            foreach (XmlListElementAttribute attribute in attributes)
            {
                SerializationMemberInfo smi2;
                ListItem listItem = new ListItem();

                smi2 = new SerializationMemberInfo(attribute.ItemType, options, cache);

                if (attribute.Alias == null)
                {
                    attribute.Alias = smi2.SerializedName;
                }

                listItem.Attribute = attribute;
                listItem.Alias     = attribute.Alias;

                // Check if a specific type of serializer is specified.

                if (attribute.SerializerType == null)
                {
                    // Figure out the serializer based on the type of the element.

                    listItem.Serializer = cache.GetTypeSerializerBySupportedType(attribute.ItemType, smi2);
                }
                else
                {
                    // Get the type of serializer they specify.

                    listItem.Serializer = cache.GetTypeSerializerBySerializerType(attribute.SerializerType, smi2);
                }

                m_TypeToItemMap[attribute.ItemType] = listItem;
                m_AliasToItemMap[attribute.Alias]   = listItem;
            }

            if (m_TypeToItemMap.Count == 0)
            {
                if (memberInfo.LogicalType.IsArray)
                {
                    ListItem listItem;
                    Type     elementType;

                    listItem = new ListItem();

                    elementType    = memberInfo.LogicalType.GetElementType();
                    listItem.Alias = elementType.Name;

                    listItem.Serializer = cache.GetTypeSerializerBySupportedType(elementType, new SerializationMemberInfo(elementType, options, cache));

                    m_TypeToItemMap[elementType]     = listItem;
                    m_AliasToItemMap[listItem.Alias] = listItem;
                }
            }

            if (m_TypeToItemMap.Count == 0)
            {
                throw new InvalidOperationException("Must specify at least one XmlListElementype.");
            }

            m_ListType = memberInfo.LogicalType;

            if (m_ListType.IsAbstract)
            {
                m_ListType = typeof(ArrayList);
            }
        }
        protected virtual void Scan(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            XmlSerializationAttribute[] attribs;

            var attributes = new List<Attribute>();

            // Get the ElementType attributes specified on the type itself as long
            // as we're not the type itself!

            if (memberInfo.MemberInfo != memberInfo.ReturnType)
            {
                var smi = new SerializationMemberInfo(memberInfo.ReturnType, options, cache);

                attribs = smi.GetApplicableAttributes(typeof(XmlListElementAttribute));

                foreach (var a in attribs)
                {
                    attributes.Add(a);
                }
            }

            // Get the ElementType attributes specified on the member.

            attribs = memberInfo.GetApplicableAttributes(typeof(XmlListElementAttribute));

            foreach (var a in attribs)
            {
                attributes.Add(a);
            }

            foreach (var attribute1 in attributes)
            {
                var attribute = (XmlListElementAttribute)attribute1;
                var listItem = new ListItem();

                if (attribute.Type == null)
                {
                    if (serializationMemberInfo.ReturnType.IsArray)
                    {
                        attribute.Type = serializationMemberInfo.ReturnType.GetElementType();
                    }
                    else if (serializationMemberInfo.ReturnType.IsGenericType)
                    {
                        attribute.Type = serializationMemberInfo.ReturnType.GetGenericArguments()[0];
                    }
                }

                var smi2 = new SerializationMemberInfo(attribute.ItemType, options, cache);

                if (attribute.Alias == null)
                {
                    attribute.Alias = smi2.SerializedName;
                }

                listItem.Attribute = attribute;
                listItem.Alias = attribute.Alias;

                // Check if a specific type of serializer is specified.

                if (attribute.SerializerType == null)
                {
                    // Figure out the serializer based on the type of the element.

                    listItem.Serializer = cache.GetTypeSerializerBySupportedType(attribute.ItemType, smi2);
                }
                else
                {
                    // Get the type of serializer they specify.

                    listItem.Serializer = cache.GetTypeSerializerBySerializerType(attribute.SerializerType, smi2);
                }

                typeToItemMap[attribute.ItemType] = listItem;
                aliasToItemMap[attribute.Alias] = listItem;
            }

            if (typeToItemMap.Count == 0)
            {
                if (memberInfo.ReturnType.IsArray)
                {
                    var listItem = new ListItem();
                    var elementType = memberInfo.ReturnType.GetElementType();
                    var sm = new SerializationMemberInfo(elementType, options, cache);

                    listItem.Alias = sm.SerializedName;

                    listItem.Serializer = cache.GetTypeSerializerBySupportedType(elementType, new SerializationMemberInfo(elementType, options, cache));

                    typeToItemMap[elementType] = listItem;
                    aliasToItemMap[listItem.Alias] = listItem;
                }
            }

            if (memberInfo.ReturnType.IsGenericType)
            {
                var elementType = memberInfo.ReturnType.GetGenericArguments()[0];

                if (!typeToItemMap.ContainsKey(elementType) && dynamicTypeResolver == null && !(elementType.IsAbstract || elementType.IsInterface))
                {
                    var listItem = new ListItem();
                    var sm = new SerializationMemberInfo(elementType, options, cache);

                    listItem.Alias = sm.SerializedName;

                    listItem.Serializer = cache.GetTypeSerializerBySupportedType(elementType, new SerializationMemberInfo(elementType, options, cache));

                    typeToItemMap[elementType] = listItem;
                    aliasToItemMap[listItem.Alias] = listItem;
                }
            }

            if (typeToItemMap.Count == 0 && this.dynamicTypeResolver == null)
            {
                throw new InvalidOperationException(
                    $"Must specify at least one XmlListElemenType or an XmlListElementTypeSerializerProvider for field {((Type)memberInfo.MemberInfo).FullName}");
            }

            listType = memberInfo.ReturnType;
        }
 public RuntimeTypeTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
 {
     verifyAttributes = (XmlSerializationAttribute[])memberInfo.GetApplicableAttributes(typeof(XmlVerifyRuntimeTypeAttribute));
 }
 public RuntimeTypeTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
 {
     verifyAttributes = (XmlSerializationAttribute[])memberInfo.GetApplicableAttributes(typeof(XmlVerifyRuntimeTypeAttribute));
 }
Example #35
0
 public ColorSerializer(Type type, SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
     : base(type, memberInfo, cache,options)
 {
 }
Example #36
0
 public abstract TypeSerializer NewTypeSerializerBySerializerType(Type serializerType, SerializationMemberInfo memberInfo, TypeSerializerCache cache);
 /// <summary>
 /// 
 /// </summary>
 /// <param name="memberInfo"></param>
 /// <param name="cache"></param>
 /// <param name="options"></param>
 public DictionaryTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
 {
 }
        /// <summary>
        /// Checks if see if an object should be serialized.
        /// </summary>
        /// <remarks>
        /// <p>
        /// An object shouldn't be serialized if it has already been serialized.
        /// This method automatically checks if the object has been serialized
        /// by examining the serialization stack.  This stack is maintained by
        /// the SerializationStart and SerializationEnd methods.
        /// </p>
        /// <p>
        /// You should call SerializationStart and SerializationEnd when you start
        /// and finish serializing an object.
        /// </p>
        /// </remarks>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool ShouldSerialize(object obj, SerializationMemberInfo memberInfo)
        {
            IXmlSerializationShouldSerializeProvider shouldSerialize;

            if (obj == null && !memberInfo.SerializeIfNull)
            {
                return false;
            }

            if ((shouldSerialize = obj as IXmlSerializationShouldSerializeProvider) != null)
            {
                if (!shouldSerialize.ShouldSerialize(this.SerializerOptions, this.Parameters))
                {
                    return false;
                }
            }

            for (var i = 0; i < stack.Count; i++)
            {
                if (stack[i] == obj)
                {
                    return false;
                }
            }

            return true;
        }
Example #39
0
        protected virtual void AddMember(MemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            SerializationMemberInfo localSerializationMemberInfo;

            if (this.MemberBound && this.serializationMemberInfo.IncludeIfUnattributed)
            {
                bool ok;
                var  fieldInfo    = memberInfo as FieldInfo;
                var  propertyInfo = memberInfo as PropertyInfo;

                if (fieldInfo != null && fieldInfo.IsPublic)
                {
                    ok = true;
                }
                else if (propertyInfo != null &&
                         propertyInfo.CanRead && propertyInfo.CanWrite &&
                         propertyInfo.GetSetMethod().IsPublic &&
                         propertyInfo.GetGetMethod().IsPublic)
                {
                    ok = true;
                }
                else
                {
                    ok = false;
                }

                if (ok)
                {
                    localSerializationMemberInfo = new SerializationMemberInfo(memberInfo, options, cache, true);

                    elementMembersMap[localSerializationMemberInfo.SerializedName] = localSerializationMemberInfo;

                    return;
                }
            }

            localSerializationMemberInfo = new SerializationMemberInfo(memberInfo, options, cache);

            if (localSerializationMemberInfo.SerializedNodeType == XmlNodeType.Element)
            {
                if (localSerializationMemberInfo.Namespace.Length > 0)
                {
                    elementMembersMap[localSerializationMemberInfo.Namespace + (char)0xff + localSerializationMemberInfo.SerializedName] = localSerializationMemberInfo;
                }
                else
                {
                    elementMembersMap[localSerializationMemberInfo.SerializedName] = localSerializationMemberInfo;
                }

                return;
            }
            else if (localSerializationMemberInfo.SerializedNodeType == XmlNodeType.Attribute)
            {
                if (localSerializationMemberInfo.Namespace.Length > 0)
                {
                    attributeMembersMap[localSerializationMemberInfo.Namespace + (char)0xff + localSerializationMemberInfo.SerializedName] = localSerializationMemberInfo;
                }
                else
                {
                    attributeMembersMap[localSerializationMemberInfo.SerializedName] = localSerializationMemberInfo;
                }
            }
            else if (localSerializationMemberInfo.SerializedNodeType == XmlNodeType.Text)
            {
                if (TextMember != null && !TextMember.Equals(localSerializationMemberInfo))
                {
                    throw new Exception(string.Format("There should only be one XmlTextAttribute in type {0}", ((Type)this.serializationMemberInfo.MemberInfo).FullName));
                }
                TextMember = localSerializationMemberInfo;
            }
        }
		protected virtual void Scan(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
		{
			XmlSerializationAttribute[] attribs;

			var attributes = new List<Attribute>();

			// Get the ElementType attributes specified on the type itself as long
			// as we're not the type itself!

			if (memberInfo.MemberInfo != memberInfo.ReturnType)
			{
				var smi = new SerializationMemberInfo(memberInfo.ReturnType, options, cache);

				attribs = smi.GetApplicableAttributes(typeof(XmlListElementAttribute));

				foreach (var a in attribs)
				{
					attributes.Add(a);
				}
			}
			
			// Get the ElementType attributes specified on the member.

			attribs = memberInfo.GetApplicableAttributes(typeof(XmlListElementAttribute));

			foreach (var a in attribs)
			{
				attributes.Add(a);
			}
			
			foreach (XmlListElementAttribute attribute in attributes)
			{
				var listItem = new ListItem();
				
				if (attribute.Type == null)
				{
					if (serializationMemberInfo.ReturnType.IsArray)
					{
						attribute.Type = serializationMemberInfo.ReturnType.GetElementType();
					}
					else if (serializationMemberInfo.ReturnType.IsGenericType)
					{
						attribute.Type = serializationMemberInfo.ReturnType.GetGenericArguments()[0];
					}
				}
			
				var smi2 = new SerializationMemberInfo(attribute.ItemType, options, cache);

				if (attribute.Alias == null)
				{
					attribute.Alias = smi2.SerializedName;
				}

				listItem.Attribute = attribute;
				listItem.Alias = attribute.Alias;

				// Check if a specific type of serializer is specified.

				if (attribute.SerializerType == null)
				{
					// Figure out the serializer based on the type of the element.

					listItem.Serializer = cache.GetTypeSerializerBySupportedType(attribute.ItemType, smi2);
				}
				else
				{
					// Get the type of serializer they specify.

					listItem.Serializer = cache.GetTypeSerializerBySerializerType(attribute.SerializerType, smi2);
				}

				typeToItemMap[attribute.ItemType] = listItem;
				aliasToItemMap[attribute.Alias] = listItem;
			}

			if (typeToItemMap.Count == 0)
			{
				if (memberInfo.ReturnType.IsArray)
				{
					var listItem = new ListItem();
					var elementType = memberInfo.ReturnType.GetElementType();
					var sm = new SerializationMemberInfo(elementType, options, cache);

					listItem.Alias = sm.SerializedName;

					listItem.Serializer = cache.GetTypeSerializerBySupportedType(elementType, new SerializationMemberInfo(elementType, options, cache));

					typeToItemMap[elementType] = listItem;
					aliasToItemMap[listItem.Alias] = listItem;
				}
			}

			if (memberInfo.ReturnType.IsGenericType)
			{
				var elementType = memberInfo.ReturnType.GetGenericArguments()[0];

				if (!typeToItemMap.ContainsKey(elementType) && dynamicTypeResolver == null && !(elementType.IsAbstract || elementType.IsInterface))
				{
					var listItem = new ListItem();
					var sm = new SerializationMemberInfo(elementType, options, cache);

					listItem.Alias = sm.SerializedName;

					listItem.Serializer = cache.GetTypeSerializerBySupportedType(elementType, new SerializationMemberInfo(elementType, options, cache));

					typeToItemMap[elementType] = listItem;
					aliasToItemMap[listItem.Alias] = listItem;
				}
			}

			if (typeToItemMap.Count == 0 && this.dynamicTypeResolver == null)
			{
			    throw new InvalidOperationException(
			        string.Format(
			            "Must specify at least one XmlListElemenType or an XmlListElementTypeSerializerProvider for field {0}",
			             ((Type)memberInfo.MemberInfo).FullName));
			}

			listType = memberInfo.ReturnType;		
		}
        public override TypeSerializer NewTypeSerializerBySupportedType(Type supportedType, SerializationMemberInfo memberInfo, TypeSerializerCache cache)
        {
            const string error = "A TypeSerializer can't be created for the given type without a memberInfo";

            if (typeof(Enum).IsAssignableFrom(supportedType))
            {
                return(new EnumTypeSerializer(memberInfo, cache, options));
            }
            else if (typeof(IDictionary).IsAssignableFrom(supportedType))
            {
                return(new DictionaryTypeSerializer(memberInfo, cache, options));
            }
            else if (typeof(Type).IsAssignableFrom(supportedType))
            {
                return(new RuntimeTypeTypeSerializer(memberInfo, cache, options));
            }
            else if (supportedType == typeof(XmlNode))
            {
                return(XmlNodeNodeTypeSerializer.Default);
            }
            else if (StringableTypeSerializer.SupportedTypes.Contains(supportedType))
            {
                return(new StringableTypeSerializer(supportedType, memberInfo, cache, options));
            }
            else if (supportedType == typeof(Guid))
            {
                return(new GuidSerializer(supportedType, memberInfo, cache, options));
            }
            else if (supportedType == typeof(Color))
            {
                return(new ColorSerializer(supportedType, memberInfo, cache, options));
            }
            else if (supportedType == typeof(DateTime))
            {
                return(new DateTimeTypeSerializer(memberInfo, cache, options));
            }
            else if (supportedType.IsGenericType && supportedType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                return(NewTypeSerializerBySupportedType
                       (
                           Nullable.GetUnderlyingType(supportedType),
                           memberInfo,
                           cache
                       ));
            }
            else
            {
                var implementsList        = false;
                var implementsGenericList = false;

                implementsList = typeof(IList).IsAssignableFrom(supportedType);

                implementsGenericList = supportedType.FindInterfaces
                                        (
                    (type, criterea) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IList <>),
                    null
                                        ).Length > 0;

                if (implementsList || implementsGenericList)
                {
                    if (memberInfo == null)
                    {
                        throw new XmlSerializerException(error);
                    }

                    return(new ListTypeSerializer(memberInfo, cache, options));
                }

                return(new ComplexTypeTypeSerializer(memberInfo, supportedType, cache, options));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="memberInfo"></param>
        /// <param name="cache"></param>
        /// <param name="options"></param>
        private void AddMember(MemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            SerializationMemberInfo serializationMemberInfo;

            serializationMemberInfo = new SerializationMemberInfo(memberInfo, options, cache);

            if (serializationMemberInfo.SerializedNodeType == XmlNodeType.Element)
            {
                if (serializationMemberInfo.Namespace.Length > 0)
                {
                    m_ElementMembersMap[serializationMemberInfo.Namespace + (char)0xff + serializationMemberInfo.SerializedName] = serializationMemberInfo;
                }
                else
                {
                    m_ElementMembersMap[serializationMemberInfo.SerializedName] = serializationMemberInfo;
                }

                return;
            }
            else  if (serializationMemberInfo.SerializedNodeType == XmlNodeType.Attribute)
            {
                if (!(serializationMemberInfo.Serializer is TypeSerializerWithSimpleTextSupport))
                {
                    throw new InvalidOperationException("Serializer for member doesn't support serializing to an attribute.");
                }

                if (serializationMemberInfo.Namespace.Length > 0)
                {
                    m_AttributeMembersMap[serializationMemberInfo.Namespace + (char)0xff + serializationMemberInfo.SerializedName] = serializationMemberInfo;
                }
                else
                {
                    m_AttributeMembersMap[serializationMemberInfo.SerializedName] = serializationMemberInfo;
                }
            }
        }
Example #43
0
 public void PushCurrentMemberInfo(SerializationMemberInfo memberInfo)
 {
     serializationMemberInfoStack.Push(memberInfo);
 }
 public abstract TypeSerializer NewTypeSerializerBySerializerType(Type serializerType, SerializationMemberInfo memberInfo, TypeSerializerCache cache);
 public LightSerializationMember(SerializationMemberInfo memberInfo)
 {
     this.Getter = memberInfo.Getter;
     this.Setter = memberInfo.Setter;
     this.Serializer = memberInfo.Serializer;
     this.MemberInfo = memberInfo.MemberInfo;
     this.SerializedName = memberInfo.GetSerializedName();
     this.SerializeAsCData = memberInfo.SerializeAsCData;
     this.XmlTreatAsNullIfEmpty = memberInfo.HasApplicableAttribute(typeof(XmlTreatAsNullIfEmptyAttribute));
     this.LogicalType = memberInfo.LogicalType;
 }