Пример #1
0
            // Standard Member
            public SerializableMember(
                XmlNodeType nodeType,
                XmlNameDeclaration memberName,
                Int32 order,
                MemberInfo member)
            {
                this.MemberName = memberName;

                this.NodeType = nodeType;
                this.Order    = order;
                if (member is PropertyInfo)
                {
                    this.CreateAccessors((PropertyInfo)member);
                }
                else if (member is FieldInfo)
                {
                    this.CreateAccessors((FieldInfo)member);
                }
                else
                {
                    throw new ArgumentException("Member type not supported.", "member");
                }

                this.serializer = XmlSerializerBase.ForType(this.MemberType);
                if (nodeType == XmlNodeType.Attribute &&
                    !(this.serializer is IXmlPrimitiveSerializer) &&
                    !(this.serializer is IXmlSerializableSerializer) &&
                    !(this.serializer is IXmlEnumSerializer))
                {
                    throw new NotSupportedException("Complex types cannot be serialized as xml attributes");
                }
                this.SelectInitializer();
            }
Пример #2
0
        static XmlObjectSerializer()
        {
            var members = ResolveFields(typeof(T))
                          .Select(field =>
                                  Handle(
                                      field,
                                      field.FieldType,
                                      field.IsPublic
                                      )
                                  ).Concat(ResolveProperties(typeof(T))
                                           // Ignore index properties
                                           .Where(property => property.GetIndexParameters().IsNullOrEmpty())
                                           .Select(property =>
                                                   Handle(
                                                       property,
                                                       property.PropertyType,
                                                       property.CanRead && property.CanWrite && (property.IsPublic() || property.HasPublicSetter())
                                                       )
                                                   )
                                           )
                          .Where(Func.NotNull)
                          .ToLookup(member => member.NodeType);

            AttributeMembers =
                members[XmlNodeType.Attribute].Where(member => !member.MatchAny).ToDictionary(member => member.MemberName);
            AnyAttributeMember =
                members[XmlNodeType.Attribute].Where(member => member.MatchAny).FirstOrDefault();
            ElementMembers =
                members[XmlNodeType.Element]
                .GroupBy(member => member.Order)
                .OrderBy(grp => grp.Key)
                .Select(grp => grp.ToList())
                .ToList();
            TextMember = members[XmlNodeType.Text].SingleOrDefault();

            var knownTypes = new Queue <XmlIncludeAttribute>(typeof(T).GetCustomAttributes <XmlIncludeAttribute>(inherit: false));

            if (knownTypes.Any())
            {
                KnownTypeSerializers       = new Dictionary <Type, Tuple <String, IXmlSerializer> >();
                KnownTypeSerializersByName = new Dictionary <String, IXmlSerializer>();
                while (knownTypes.Any())
                {
                    var knownType  = knownTypes.Dequeue();
                    var serializer = XmlSerializerBase.ForType(knownType.Type);
                    // TODO figure out the type name mapping for primitives.
                    var typeName = knownType.Type.Name;
                    KnownTypeSerializers.Add(knownType.Type, Tuple.Create(typeName, serializer));
                    KnownTypeSerializersByName.Add(typeName, serializer);

                    foreach (var subKnownType in knownType.Type.GetCustomAttributes <XmlIncludeAttribute>(inherit: false))
                    {
                        knownTypes.Enqueue(subKnownType);
                    }
                }
            }
        }
Пример #3
0
            // XmlAnyAttribute or XmlAnyElement member
            public SerializableMember(
                XmlNodeType nodeType,
                Int32 order,
                MemberInfo member)
            {
                this.NodeType = nodeType;
                this.Order    = order;
                if (member is PropertyInfo)
                {
                    CreateAccessors((PropertyInfo)member);
                }
                else if (member is FieldInfo)
                {
                    CreateAccessors((FieldInfo)member);
                }
                else
                {
                    throw new ArgumentException("Member type not supported.", "member");
                }

                this.MatchAny   = true;
                this.serializer = XmlSerializerBase.ForType(this.MemberType);
                this.SelectInitializer();
            }
Пример #4
0
 private static IXmlSerializer <T> CreateSerializer <T>()
 {
     return(XmlSerializerBase <T> .CreateSerializer());
 }