예제 #1
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);
        }
예제 #2
0
        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);
        }
        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 SerializationMemberInfo(MemberInfo memberInfo, SerializerOptions options, TypeSerializerCache cache, bool includeIfUnattributed)
        {
            typeSerializerCache = cache;
            this.memberInfo = memberInfo;

            this.includeIfUnattributed = includeIfUnattributed;

            Scan(options, includeIfUnattributed);
        }
예제 #5
0
        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="type"></param>
        /// <param name="cache"></param>
        /// <param name="options"></param>
        public ComplexTypeTypeSerializer(Type type, TypeSerializerCache cache, SerializerOptions options)
        {
            m_Type = type;

            m_ElementMembersMap = new Hashtable(0x10);
            m_AttributeMembersMap = new Hashtable(0x10);

            cache.Add(this);

            Scan(cache, options);
        }
예제 #9
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="cache"></param>
        /// <param name="options"></param>
        public AnyTypeTypeSerializer(Type type, TypeSerializerCache cache, SerializerOptions options)
        {
            m_Type = type;

            m_ElementMembersMap = new SortedList(0x10);
            m_AttributeMembersMap = new SortedList(0x10);

            cache.Add(this);

            Scan(cache, options);
        }
        /// <summary>
        /// Tests if this attribute should be applied/considered when serializing.
        /// </summary>
        /// <param name="options"></param>
        internal virtual bool Applies(SerializerOptions options)
        {
            if (Constraints.Length > 0)
            {
                var constraints = Constraints.Split(';');

                foreach (string s in constraints)
                {
                    object value;
                    var forNotEquals = true;

                    var namevalue = s.Split(new string[] { "!=" }, StringSplitOptions.None);

                    if (namevalue.Length < 2)
                    {
                        namevalue = s.Split('=');

                        forNotEquals = false;
                    }

                    if (!options.TryGetValue(namevalue[0], out value))
                    {
                        if (forNotEquals)
                        {
                            return true;

                        }
                        return false;
                    }

                    if (forNotEquals)
                    {
                        if (value.ToString().EqualsIgnoreCase(namevalue[1]))
                        {
                            return false;
                        }
                    }
                    else
                    {
                        if (!value.ToString().EqualsIgnoreCase(namevalue[1]))
                        {
                            return false;
                        }
                    }
                }
            }

            return true;
        }
        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);
        }
        /// <summary>
        /// Tests if this attribute should be applied/considered when serializing.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public virtual bool Applies(SerializerOptions options)
        {
            if (m_ConditionName.Length > 0)
            {
                object o = options[m_ConditionName];

                if (o == null)
                {
                    return false;
                }

                if (!o.Equals(m_ConditionValue))
                {
                    return false;
                }
            }

            return true;
        }
 public SerializationContext(SerializerOptions options, SerializationParameters parameters)
 {
     this.Parameters = parameters;
     SerializerOptions = options;
 }
        /// <summary>
        /// Scan the tyoe for properties and fields to serialize.
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="options"></param>
        protected virtual void Scan(TypeSerializerCache cache, SerializerOptions options)
        {
            var type = supportedType;

            while (type != typeof(object) && type != null)
            {
                var fields = supportedType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
                var properties = supportedType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

                foreach (var field in fields)
                {
                    AddMember(field, cache, options);
                }

                foreach (var property in properties)
                {
                    AddMember(property, cache, options);
                }

                var serializeBase = true;
                var attribs = type.GetCustomAttributes(typeof(XmlSerializeBaseAttribute), false);

                foreach (XmlSerializeBaseAttribute attrib in attribs)
                {
                    if (attrib.Applies(options))
                    {
                        serializeBase = attrib.SerializeBase;
                    }
                }

                if (!serializeBase)
                {
                    break;
                }

                type = type.BaseType;
            }
        }
예제 #15
0
 /// <summary>
 /// Creates a new <see cref="XmlSerializer{T}"/>
 /// </summary>
 /// <param name="type">The type supported by the serializer</param>
 /// <param name="options">The options for the serializer</param>
 /// <param name="formatting">The formatting for the serializer.Formatting</param>
 /// <returns>A new <see cref="XmlSerializer{T}"/></returns>
 public abstract XmlSerializer <object> NewXmlSerializer(Type type, SerializerOptions options, Formatting formatting);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="options"></param>
        private void Scan(TypeSerializerCache cache, SerializerOptions options)
        {
            Type type;
            FieldInfo[] fields;
            PropertyInfo[] properties;

            type = m_Type;

            while (type != typeof(object) && type != null)
            {
                fields = m_Type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
                properties = m_Type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

                foreach (FieldInfo field in fields)
                {
                    AddMember(field, cache, options);
                }

                foreach (PropertyInfo property in properties)
                {
                    AddMember(property, cache, options);
                }

                object[] attribs;
                bool serializeBase = true;

                attribs = type.GetCustomAttributes(typeof(XmlSerializeBaseAttribute), false);

                foreach (XmlSerializeBaseAttribute attrib in attribs)
                {
                    if (attrib.Applies(options))
                    {
                        if (attrib.SerializeBase)
                        {
                            serializeBase = true;
                        }
                    }
                }

                if (!serializeBase)
                {
                    break;
                }

                type = type.BaseType;
            }
        }
예제 #17
0
 public ColorSerializer(Type type, SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
     : base(type, memberInfo, cache,options)
 {
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="memberInfo"></param>
 /// <param name="cache"></param>
 /// <param name="options"></param>
 public DictionaryTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
 {
 }
예제 #19
0
 public SerializationContext(SerializerOptions options, SerializationParameters parameters)
 {
     this.Parameters   = parameters;
     SerializerOptions = options;
 }
 public RuntimeTypeTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
 {
     verifyAttributes = (XmlSerializationAttribute[])memberInfo.GetApplicableAttributes(typeof(XmlVerifyRuntimeTypeAttribute));
 }
 public SerializationMemberInfo(MemberInfo memberInfo, SerializerOptions options, TypeSerializerCache cache)
     : this(memberInfo, options, cache, false)
 {
 }
        private static XmlSerializationAttribute[] ExtractApplicableAttributes(object[] attributes, SerializerOptions options)
        {
            ArrayList list = new ArrayList();

            foreach (XmlSerializationAttribute attribute in attributes)
            {
                if (attribute.Applies(options))
                {
                    list.Add(attribute);
                }
            }

            return((XmlSerializationAttribute[])list.ToArray(typeof(XmlSerializationAttribute)));
        }
        /// <summary>
        /// Prescans the type.
        /// </summary>
        protected virtual void Scan(SerializerOptions options, bool includeIfUnattributed)
        {
            XmlApproachAttribute approach = null;
            XmlElementAttribute  elementAttribute;

            // Get the applicable attributes

            LoadAttributes(options);

            // Get the setter/getter and serializer

            if (memberInfo is FieldInfo)
            {
                getterSetter = new FieldGetterSetter(memberInfo);

                returnType = ((FieldInfo)memberInfo).FieldType;
            }
            else if (memberInfo is PropertyInfo)
            {
                var propertyInfo = (PropertyInfo)memberInfo;

                getterSetter = new PropertyGetterSetter(memberInfo);

                returnType = ((PropertyInfo)memberInfo).PropertyType;
            }
            else if (memberInfo is Type)
            {
                getterSetter = null;

                serializedName = memberInfo.Name;
                returnType     = (Type)memberInfo;
            }
            else
            {
                throw new ArgumentException(String.Format("Unsupported member type: {0}", memberInfo.MemberType.ToString()));
            }

            // Get the [XmlExclude] [XmlAttribute] or [XmlElement] attribute

            var attribute = GetFirstApplicableAttribute(false, typeof(XmlExcludeAttribute), typeof(XmlTextAttribute), typeof(XmlAttributeAttribute), typeof(XmlElementAttribute));

            if (attribute != null)
            {
                if (attribute is XmlExcludeAttribute)
                {
                    // This member needs to be excluded

                    serializedNodeType = XmlNodeType.None;
                }
                else if (attribute is XmlTextAttribute)
                {
                    serializedNodeType = XmlNodeType.Text;
                }
                else if ((approach = attribute as XmlApproachAttribute) != null)
                {
                    ApproachAttribute = approach;

                    // This member needs to be included as an attribute or an element

                    serializedNodeType = approach is XmlElementAttribute ? XmlNodeType.Element : XmlNodeType.Attribute;

                    if (approach.Type != null)
                    {
                        returnType = approach.Type;
                    }

                    serializedName      = approach.Name;
                    serializedNamespace = approach.Namespace;

                    if ((elementAttribute = approach as XmlElementAttribute) != null)
                    {
                        if (elementAttribute.SerializeAsValueNode)
                        {
                            serializeAsValueNodeAttributeName = elementAttribute.ValueNodeAttributeName;
                        }
                    }

                    if (approach.SerializeUnattribted)
                    {
                        this.includeIfUnattributed = true;
                    }

                    if (approach.SerializeIfNull)
                    {
                        this.serializeIfNull = true;
                    }
                }
            }
            else
            {
                if (includeIfUnattributed)
                {
                    serializedName = memberInfo.Name;

                    serializedNodeType = XmlNodeType.Element;
                }
                else
                {
                    serializedNodeType = XmlNodeType.None;
                }
            }

            if (serializedNodeType == XmlNodeType.None)
            {
                return;
            }

            // Check if the member should be serialized as CDATA

            attribute = GetFirstApplicableAttribute(typeof(XmlCDataAttribute));

            if (attribute != null)
            {
                serializeAsCData = ((XmlCDataAttribute)attribute).Enabled;
            }

            attribute = GetFirstApplicableAttribute(typeof(XmlVariableSubstitutionAttribute));

            if (attribute != null)
            {
                Substitutor = (IVariableSubstitutor)Activator.CreateInstance(((XmlVariableSubstitutionAttribute)attribute).SubstitutorType);
            }

            // Set the serialized (element or attribute) name to the name of the member if it hasn't already been set

            if (serializedName.Length == 0)
            {
                if (approach != null && approach.UseNameFromAttributedType && memberInfo.MemberType == MemberTypes.TypeInfo)
                {
                    serializedName = GetAttributeDeclaringType((Type)memberInfo, approach).Name;
                }
                else
                {
                    serializedName = this.memberInfo.Name.Left(PredicateUtils.ObjectEquals('`').Not());
                }
            }

            // Make the serialized (element or attribute) name lowercase if requested

            if (approach != null)
            {
                if (approach.MakeNameLowercase)
                {
                    serializedName = serializedName.ToLower();
                }
            }

            // Get the explicitly specified TypeSerializer if requested

            attribute = GetFirstApplicableAttribute(typeof(XmlTypeSerializerTypeAttribute));

            if (attribute != null)
            {
                if (((XmlTypeSerializerTypeAttribute)attribute).SerializerType != null)
                {
                    typeSerializer = typeSerializerCache.GetTypeSerializerBySerializerType(((XmlTypeSerializerTypeAttribute)attribute).SerializerType, this);

                    if (!returnType.IsAssignableFrom(typeSerializer.SupportedType))
                    {
                        throw new InvalidOperationException(String.Format("Explicitly specified serializer ({0}) doesn't support serializing of associated program element.", ((XmlTypeSerializerTypeAttribute)attribute).SerializerType.Name));
                    }
                }
            }
            else
            {
                typeSerializer = typeSerializerCache.GetTypeSerializerBySupportedType(returnType, this);
            }

            // Check if the member should be treated as a null value if it is empty

            treatAsNullIfEmpty = HasApplicableAttribute(typeof(XmlTreatAsNullIfEmptyAttribute));

            // Check if the member's declared type is polymorphic

            var polymorphicTypeAttribute = (XmlPolymorphicTypeAttribute)GetFirstApplicableAttribute(typeof(XmlPolymorphicTypeAttribute));

            if (polymorphicTypeAttribute != null)
            {
                polymorphicTypeProvider = (IXmlDynamicTypeProvider)Activator.CreateInstance(polymorphicTypeAttribute.PolymorphicTypeProvider);
            }
        }
예제 #24
0
        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);
        }
예제 #25
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);
            }
        }
예제 #26
0
        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;
        }
예제 #27
0
 /// <summary>
 /// Constructs a new XmlSerializer supporting type <see cref="T"/> with the provided options
 /// </summary>
 /// <param name="options">The options</param>
 internal XmlSerializer(SerializerOptions options)
     : this(typeof(T), options)
 {
 }
예제 #28
0
 public ColorSerializer(Type type, SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
     : base(type, memberInfo, cache, options)
 {
 }
예제 #29
0
 /// <summary>
 /// Creates an XmlSerializer for the supplied type <see cref="type"/> using the default <see cref="XmlSerializerFactory"/>
 /// and given <see cref="SerializerOptions"/>
 /// </summary>
 public static XmlSerializer <object> New(Type type, SerializerOptions options)
 {
     return(XmlSerializerFactory.Default.NewXmlSerializer(type, options));
 }
 public SerializationMemberInfo(MemberInfo memberInfo, SerializerOptions options, TypeSerializerCache cache)
     : this(memberInfo, options, cache, false)
 {
 }
        /// <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;
                }
            }
        }
 public RuntimeTypeTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
 {
     verifyAttributes = (XmlSerializationAttribute[])memberInfo.GetApplicableAttributes(typeof(XmlVerifyRuntimeTypeAttribute));
 }
예제 #33
0
 /// <summary>
 /// Creates an XmlSerializer for the supplied type <see cref="T"/> using the default <see cref="XmlSerializerFactory"/>.
 /// </summary>
 public static XmlSerializer <T> New(SerializerOptions options)
 {
     return(XmlSerializerFactory.Default.NewXmlSerializer <T>(options));
 }
        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;
            }
        }
        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;
            }
        }
예제 #36
0
 /// <summary>
 /// Creates a new <see cref="XmlSerializer{T}"/>
 /// </summary>
 /// <param name="type">The type supported by the serializer</param>
 /// <param name="options">The options for the serializer</param>
 /// <returns>A new <see cref="XmlSerializer{T}"/></returns>
 public abstract XmlSerializer <object> NewXmlSerializer(Type type, SerializerOptions options);
예제 #37
0
        public DateTimeTypeSerializer(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
        {
            formatAttribute = (XmlDateTimeFormatAttribute)memberInfo.GetFirstApplicableAttribute(typeof(XmlDateTimeFormatAttribute));

            if (formatAttribute == null)
            {
                formatAttribute = new XmlDateTimeFormatAttribute("G");
                formatSpecified = false;
            }
            else
            {
                formatSpecified = true;
            }
        }
 public SqlDatabaseContextInfoDynamicTypeProvider(SerializationMemberInfo memberInfo, TypeSerializerCache cache, SerializerOptions options)
 {
 }
        /// <summary>
        /// Prescans the type.
        /// </summary>
        protected virtual void Scan(SerializerOptions options, bool includeIfUnattributed)
        {
            XmlApproachAttribute approach = null;
            XmlElementAttribute elementAttribute;

            // Get the applicable attributes

            LoadAttributes(options);

            // Get the setter/getter and serializer

            if (memberInfo is FieldInfo)
            {
                getterSetter = new FieldGetterSetter(memberInfo);

                returnType = ((FieldInfo) memberInfo).FieldType;
            }
            else if (memberInfo is PropertyInfo)
            {
                var propertyInfo = (PropertyInfo) memberInfo;

                getterSetter = new PropertyGetterSetter(memberInfo);

                returnType = ((PropertyInfo) memberInfo).PropertyType;
            }
            else if (memberInfo is Type)
            {
                getterSetter = null;

                serializedName = memberInfo.Name;
                returnType = (Type) memberInfo;
            }
            else
            {
                throw new ArgumentException($"Unsupported member type: {this.memberInfo.MemberType.ToString()}");
            }

            // Get the [XmlExclude] [XmlAttribute] or [XmlElement] attribute

            var attribute = GetFirstApplicableAttribute(false, typeof (XmlExcludeAttribute),typeof(XmlTextAttribute), typeof (XmlAttributeAttribute), typeof (XmlElementAttribute));

            if (attribute != null)
            {
                if (attribute is XmlExcludeAttribute)
                {
                    // This member needs to be excluded

                    serializedNodeType = XmlNodeType.None;
                }
                else if (attribute is XmlTextAttribute)
                {
                    serializedNodeType = XmlNodeType.Text;
                }
                else if ((approach = attribute as XmlApproachAttribute) != null)
                {
                    ApproachAttribute = approach;

                    // This member needs to be included as an attribute or an element

                    serializedNodeType = approach is XmlElementAttribute ? XmlNodeType.Element : XmlNodeType.Attribute;

                    if (approach.Type != null)
                    {
                        returnType = approach.Type;
                    }

                    serializedName = approach.Name;
                    serializedNamespace = approach.Namespace;

                    if ((elementAttribute = approach as XmlElementAttribute) != null)
                    {
                        if (elementAttribute.SerializeAsValueNode)
                        {
                            serializeAsValueNodeAttributeName = elementAttribute.ValueNodeAttributeName;
                        }
                    }

                    if (approach.SerializeUnattribted)
                    {
                        this.includeIfUnattributed = true;
                    }

                    if (approach.SerializeIfNull)
                        this.serializeIfNull = true;
                }
            }
            else
            {
                if (includeIfUnattributed)
                {
                    serializedName = memberInfo.Name;

                    serializedNodeType = XmlNodeType.Element;
                }
                else
                {
                    serializedNodeType = XmlNodeType.None;
                }
            }

            if (serializedNodeType == XmlNodeType.None)
            {
                return;
            }

            // Check if the member should be serialized as CDATA

            attribute = GetFirstApplicableAttribute(typeof (XmlCDataAttribute));

            if (attribute != null)
            {
                serializeAsCData = ((XmlCDataAttribute) attribute).Enabled;
            }

            attribute = GetFirstApplicableAttribute(typeof (XmlVariableSubstitutionAttribute));

            if (attribute != null)
            {
                Substitutor = (IVariableSubstitutor) Activator.CreateInstance(((XmlVariableSubstitutionAttribute) attribute).SubstitutorType);
            }

            // Set the serialized (element or attribute) name to the name of the member if it hasn't already been set

            if (serializedName.Length == 0)
            {
                if (approach != null && approach.UseNameFromAttributedType && memberInfo.MemberType == MemberTypes.TypeInfo)
                {
                    serializedName = GetAttributeDeclaringType((Type) memberInfo, approach).Name;
                }
                else
                {
                    serializedName = this.memberInfo.Name.Left(PredicateUtils.ObjectEquals('`').Not());
                }
            }

            // Make the serialized (element or attribute) name lowercase if requested

            if (approach != null)
            {
                if (approach.MakeNameLowercase)
                {
                    serializedName = serializedName.ToLower();
                }
            }

            // Get the explicitly specified TypeSerializer if requested

            attribute = GetFirstApplicableAttribute(typeof (XmlTypeSerializerTypeAttribute));

            if (attribute != null)
            {
                if (((XmlTypeSerializerTypeAttribute) attribute).SerializerType != null)
                {
                    typeSerializer = typeSerializerCache.GetTypeSerializerBySerializerType(((XmlTypeSerializerTypeAttribute) attribute).SerializerType, this);

                    if (!returnType.IsAssignableFrom(typeSerializer.SupportedType))
                    {
                        throw new InvalidOperationException($"Explicitly specified serializer ({((XmlTypeSerializerTypeAttribute)attribute).SerializerType.Name}) doesn't support serializing of associated program element.");
                    }
                }
            }
            else
            {
                typeSerializer = typeSerializerCache.GetTypeSerializerBySupportedType(returnType, this);
            }

            // Check if the member should be treated as a null value if it is empty

            treatAsNullIfEmpty = HasApplicableAttribute(typeof (XmlTreatAsNullIfEmptyAttribute));

            // Check if the member's declared type is polymorphic

            var polymorphicTypeAttribute = (XmlPolymorphicTypeAttribute) GetFirstApplicableAttribute(typeof (XmlPolymorphicTypeAttribute));

            if (polymorphicTypeAttribute != null)
            {
                polymorphicTypeProvider = (IXmlDynamicTypeProvider) Activator.CreateInstance(polymorphicTypeAttribute.PolymorphicTypeProvider);
            }
        }
예제 #40
0
        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;
            }
        }
        private static XmlSerializationAttribute[] ExtractApplicableAttributes(object[] attributes, SerializerOptions options)
        {
            ArrayList list = new ArrayList();

            foreach (XmlSerializationAttribute attribute in attributes)
            {
                if (attribute.Applies(options))
                {
                    list.Add(attribute);
                }
            }

            return (XmlSerializationAttribute[]) list.ToArray(typeof (XmlSerializationAttribute));
        }
예제 #42
0
        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);
        }
        private void LoadAttributes(SerializerOptions options)
        {
            applicableMemberAttributes = ExtractApplicableAttributes(GetCustomAttributes(memberInfo, typeof (XmlSerializationAttribute), true), options);

            if (memberInfo is Type)
            {
                applicableTypeAttributes = applicableMemberAttributes;
            }
            else
            {
                applicableTypeAttributes = ExtractApplicableAttributes(GetCustomAttributes(GetDeclaredType(memberInfo), typeof (XmlSerializationAttribute), true), options);
            }
        }
예제 #44
0
        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;
        }
예제 #45
0
        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");
            }
        }
예제 #46
0
 /// <summary>
 /// Creates a new <see cref="XmlSerializer{T}"/>
 /// </summary>
 /// <typeparam name="T">The serializer's supported object type</typeparam>
 /// <param name="options">The options for the serializer</param>
 /// <returns>A new <see cref="XmlSerializer{T}"/></returns>
 public abstract XmlSerializer <T> NewXmlSerializer <T>(SerializerOptions options);