Exemplo n.º 1
0
        protected internal override void Initialize(IntermediateSerializer serializer)
        {
            _keySerializer   = serializer.GetTypeSerializer(typeof(TKey));
            _valueSerializer = serializer.GetTypeSerializer(typeof(TValue));

            _keyFormat = new ContentSerializerAttribute
            {
                ElementName = "Key",
                AllowNull   = false
            };

            _valueFormat = new ContentSerializerAttribute()
            {
                ElementName = "Value",
                AllowNull   = typeof(TValue).IsValueType
            };
        }
Exemplo n.º 2
0
        public static T Deserialize <T>(XmlReader input, string referenceRelocationPath)
        {
            var serializer = new IntermediateSerializer();
            var reader     = new IntermediateReader(serializer, input, referenceRelocationPath);
            var asset      = default(T);

            try
            {
                if (!reader.MoveToElement("XnaContent"))
                {
                    throw new InvalidContentException(string.Format("Could not find XnaContent element in '{0}'.",
                                                                    referenceRelocationPath));
                }

                // Initialize the namespace lookups from
                // the attributes on the XnaContent element.
                serializer.CreateNamespaceLookup(input);

                // Move past the XnaContent.
                input.ReadStartElement();

                // Read the asset.
                var format = new ContentSerializerAttribute {
                    ElementName = "Asset"
                };
                asset = reader.ReadObject <T>(format);

                // Process the shared resources and external references.
                reader.ReadSharedResources();
                reader.ReadExternalReferences();

                // Move past the closing XnaContent element.
                input.ReadEndElement();
            }
            catch (XmlException xmlException)
            {
                throw reader.NewInvalidContentException(xmlException, "An error occured parsing.");
            }

            return(asset);
        }
Exemplo n.º 3
0
        public static void Serialize <T>(XmlWriter output, T value, string referenceRelocationPath)
        {
            var serializer = new IntermediateSerializer();
            var writer     = new IntermediateWriter(serializer, output, referenceRelocationPath);

            output.WriteStartElement("XnaContent");

            // TODO: Write namespaces?

            // Write the asset.
            var format = new ContentSerializerAttribute {
                ElementName = "Asset"
            };

            writer.WriteObject(value, format);

            // TODO: Write the shared resources and external
            // references here!

            // Close the XnaContent element.
            output.WriteEndElement();
        }
Exemplo n.º 4
0
        public static void Serialize <T>(XmlWriter output, T value, string referenceRelocationPath)
        {
            var serializer = new IntermediateSerializer();
            var writer     = new IntermediateWriter(serializer, output, referenceRelocationPath);

            output.WriteStartElement("XnaContent");

            serializer._namespaceAliasHelper.WriteNamespaces(output, value);

            // Write the asset.
            var format = new ContentSerializerAttribute {
                ElementName = "Asset"
            };

            writer.WriteObjectInternal(value, format, serializer.GetTypeSerializer(typeof(T)), typeof(object));

            // Process the shared resources and external references.
            writer.WriteSharedResources();
            writer.WriteExternalReferences();

            // Close the XnaContent element.
            output.WriteEndElement();
        }
        protected internal override void Initialize(IntermediateSerializer serializer)
        {
            // If we have a base type then we need to deserialize it first.
            if (TargetType.BaseType != null)
            {
                _baseSerializer = serializer.GetTypeSerializer(TargetType.BaseType);
            }

            // Cache all our serializable properties.
            var properties = TargetType.GetProperties(_bindingFlags);

            foreach (var prop in properties)
            {
                ElementInfo info;
                if (GetElementInfo(serializer, prop, out info))
                {
                    _elements.Add(info);
                }
            }

            // Cache all our serializable fields.
            var fields = TargetType.GetFields(_bindingFlags);

            foreach (var field in fields)
            {
                ElementInfo info;
                if (GetElementInfo(serializer, field, out info))
                {
                    _elements.Add(info);
                }
            }

            if (GenericCollectionHelper.IsGenericCollectionType(TargetType, false))
            {
                _collectionHelper = serializer.GetCollectionHelper(TargetType);
            }
        }
Exemplo n.º 6
0
        protected internal override void Initialize(IntermediateSerializer serializer)
        {
            var properties = TargetType.GetAllProperties();

            foreach (var prop in properties)
            {
                ElementInfo info;
                if (GetElementInfo(serializer, prop, out info))
                {
                    _elements.Add(info.Name, info);
                }
            }

            var fields = TargetType.GetAllFields();

            foreach (var field in fields)
            {
                ElementInfo info;
                if (GetElementInfo(serializer, field, out info))
                {
                    _elements.Add(info.Name, info);
                }
            }
        }
Exemplo n.º 7
0
 protected internal override void Initialize(IntermediateSerializer serializer)
 {
     _serializer = (ElementSerializer <T>)serializer.GetTypeSerializer(typeof(T));
 }
Exemplo n.º 8
0
 protected internal virtual void ScanChildren(IntermediateSerializer serializer, ChildCallback callback, object value)
 {
 }
Exemplo n.º 9
0
 protected internal override void ScanChildren(IntermediateSerializer serializer, ChildCallback callback, object value)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 10
0
        protected internal override void ScanChildren(IntermediateSerializer serializer, ChildCallback callback, object value)
        {
            var cast = value == null ? default(T) : (T)value;

            ScanChildren(serializer, callback, cast);
        }
Exemplo n.º 11
0
        private bool GetElementInfo(IntermediateSerializer serializer, MemberInfo member, out ElementInfo info)
        {
            info = new ElementInfo();

            // Are we ignoring this property?
            if (ReflectionHelpers.GetCustomAttribute(member, typeof(ContentSerializerIgnoreAttribute)) != null)
            {
                return(false);
            }

            var prop  = member as PropertyInfo;
            var field = member as FieldInfo;

            var attrib = ReflectionHelpers.GetCustomAttribute(member, typeof(ContentSerializerAttribute)) as ContentSerializerAttribute;

            if (attrib != null)
            {
                // Store the attribute for later use.
                info.Attribute = attrib.Clone();

                // Default the to member name as the element name.
                if (string.IsNullOrEmpty(attrib.ElementName))
                {
                    info.Attribute.ElementName = member.Name;
                }
            }
            else
            {
                // We don't have a serializer attribute, so we can
                // only access this member thru a public field/property.

                if (prop != null)
                {
                    // If we don't have at least a public getter then this
                    // property can't be serialized or deserialized in any way.
                    if (prop.GetGetMethod() == null)
                    {
                        return(false);
                    }

                    // If there is no public setter and the property is a system
                    // type then we have no way for it to be deserialized.
                    if (prop.GetSetMethod() == null &&
                        prop.PropertyType.Namespace == "System")
                    {
                        return(false);
                    }
                }
                else if (field != null)
                {
                    if (!field.IsPublic)
                    {
                        return(false);
                    }
                }

                info.Attribute             = new ContentSerializerAttribute();
                info.Attribute.ElementName = member.Name;
            }

            if (prop != null)
            {
                info.Serializer = serializer.GetTypeSerializer(prop.PropertyType);
                if (prop.CanWrite)
                {
                    info.Setter = (o, v) => prop.SetValue(o, v, null);
                }
                info.Getter = (o) => prop.GetValue(o, null);
            }
            else if (field != null)
            {
                info.Serializer = serializer.GetTypeSerializer(field.FieldType);
                info.Setter     = field.SetValue;
                info.Getter     = field.GetValue;
            }

            return(true);
        }
Exemplo n.º 12
0
 internal IntermediateReader(IntermediateSerializer serializer, XmlReader xml)
 {
 }
Exemplo n.º 13
0
 internal IntermediateWriter(IntermediateSerializer serializer, XmlWriter xmlWriter, string filePath)
 {
     Serializer = serializer;
     Xml        = xmlWriter;
     _filePath  = filePath;
 }
Exemplo n.º 14
0
 public NamespaceAliasHelper(IntermediateSerializer serializer)
 {
     _serializer = serializer;
 }
Exemplo n.º 15
0
 internal IntermediateWriter(IntermediateSerializer serializer, XmlWriter xml)
 {
 }
Exemplo n.º 16
0
 protected internal override void Initialize(IntermediateSerializer serializer)
 {
     _itemSerializer = serializer.GetTypeSerializer(typeof(T));
 }
Exemplo n.º 17
0
 protected internal override void Initialize(IntermediateSerializer serializer)
 {
     _listSerializer.Initialize(serializer);
 }
Exemplo n.º 18
0
        private bool GetElementInfo(IntermediateSerializer serializer, MemberInfo member, out ElementInfo info)
        {
            info = new ElementInfo();

            // Are we ignoring this property?
            if (ReflectionHelpers.GetCustomAttribute(member, typeof(ContentSerializerIgnoreAttribute)) != null)
            {
                return(false);
            }

            var prop  = member as PropertyInfo;
            var field = member as FieldInfo;

            // If we can write or read from it we can skip it.
            if (prop != null && (!prop.CanWrite || !prop.CanRead))
            {
                return(false);
            }

            // Default the to member name as the element name.
            info.Name = member.Name;

            var attrib = ReflectionHelpers.GetCustomAttribute(member, typeof(ContentSerializerAttribute)) as ContentSerializerAttribute;

            if (attrib != null)
            {
                if (!string.IsNullOrEmpty(attrib.ElementName))
                {
                    info.Name = attrib.ElementName;
                }
            }
            else if (prop != null)
            {
                if (!ReflectionHelpers.PropertyIsPublic(prop))
                {
                    return(false);
                }
            }
            else if (field != null)
            {
                if (!field.IsPublic)
                {
                    return(false);
                }
            }

            if (prop != null)
            {
                info.Serializer = serializer.GetTypeSerializer(prop.PropertyType);
                info.Setter     = (o, v) => prop.SetValue(o, v, null);
                info.Getter     = (o) => prop.GetValue(o, null);
            }
            else if (field != null)
            {
                info.Serializer = serializer.GetTypeSerializer(field.FieldType);
                info.Setter     = field.SetValue;
                info.Getter     = field.GetValue;
            }

            return(true);
        }
Exemplo n.º 19
0
 protected internal override void ScanChildren(IntermediateSerializer serializer, ChildCallback callback, T[] value)
 {
     _listSerializer.ScanChildren(serializer, callback, new List <T>(value));
 }
Exemplo n.º 20
0
        private bool GetElementInfo(IntermediateSerializer serializer, MemberInfo member, out ElementInfo info)
        {
            info = new ElementInfo();

            // Are we ignoring this property?
            if (ReflectionHelpers.GetCustomAttribute <ContentSerializerIgnoreAttribute>(member) != null)
            {
                return(false);
            }

            var prop  = member as PropertyInfo;
            var field = member as FieldInfo;

            var attrib = ReflectionHelpers.GetCustomAttribute <ContentSerializerAttribute>(member);

            if (attrib != null)
            {
                // Store the attribute for later use.
                info.Attribute = attrib.Clone();

                // Default the to member name as the element name.
                if (string.IsNullOrEmpty(attrib.ElementName))
                {
                    info.Attribute.ElementName = member.Name;
                }
            }
            else
            {
                // We don't have a serializer attribute, so we can
                // only access this member thru a public field/property.

                if (prop != null)
                {
                    // If we don't have at least a public getter then this
                    // property can't be serialized or deserialized in any way.
                    if (prop.GetGetMethod() == null)
                    {
                        return(false);
                    }

                    // If there is a setter, but it's private, then don't include this element
                    // (although technically we could, as long as we have a serializer with
                    // CanDeserializeIntoExistingObject=true for this property type)
                    var setter = prop.GetSetMethod(true);
                    if (setter != null && !setter.IsPublic)
                    {
                        return(false);
                    }

                    // If there is no setter, and we don't have a type serializer
                    // that can deserialize into an existing object, then we have no way
                    // for it to be deserialized.
                    if (setter == null && !serializer.GetTypeSerializer(prop.PropertyType).CanDeserializeIntoExistingObject)
                    {
                        return(false);
                    }

                    // Don't serialize or deserialize indexers.
                    if (prop.GetIndexParameters().Any())
                    {
                        return(false);
                    }
                }
                else if (field != null)
                {
                    if (!field.IsPublic)
                    {
                        return(false);
                    }
                }

                info.Attribute             = new ContentSerializerAttribute();
                info.Attribute.ElementName = member.Name;
            }

            if (prop != null)
            {
                info.Serializer = serializer.GetTypeSerializer(prop.PropertyType);
                if (prop.CanWrite)
                {
                    info.Setter = (o, v) => prop.SetValue(o, v, null);
                }
                info.Getter = o => prop.GetValue(o, null);
            }
            else if (field != null)
            {
                info.Serializer = serializer.GetTypeSerializer(field.FieldType);
                info.Setter     = field.SetValue;
                info.Getter     = field.GetValue;
            }

            return(true);
        }
Exemplo n.º 21
0
 protected internal override void ScanChildren(IntermediateSerializer serializer, ChildCallback callback, Object value)
 {
     ScanChildren(serializer, callback, (T)value);
 }
Exemplo n.º 22
0
 protected internal virtual void ScanChildren(IntermediateSerializer serializer, ChildCallback callback, T value)
 {
     base.ScanChildren(serializer, callback, value);
 }
Exemplo n.º 23
0
 protected internal virtual void Initialize(IntermediateSerializer serializer)
 {
 }