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 }; }
public static T Deserialize <T>(XmlReader input, string referenceRelocationPath) { if (input == null) { throw new ArgumentNullException(nameof(input)); } var serializer = new IntermediateSerializer(); var reader = new IntermediateReader(serializer, input, referenceRelocationPath); T asset; try { if (!reader.MoveToElement("XnaContent")) { throw new InvalidContentException( $"Could not find XnaContent element in '{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.Exception_InvalidContent(xmlException, "An error occured parsing."); } return(asset); }
public GenericCollectionHelper(IntermediateSerializer serializer, Type type) { var collectionElementType = GetCollectionElementType(type, false); if (collectionElementType == null) throw new ArgumentException($"Failed to get collection element type of {type}."); _contentSerializer = serializer.GetTypeSerializer(collectionElementType); var collectionType = typeof(ICollection<>).MakeGenericType(collectionElementType); var countProperty = collectionType.GetProperty("Count"); if (countProperty == null) throw new Exception("Failed to get property for reflection."); _countProperty = countProperty; var addMethod = collectionType.GetMethod("Add", new[] { collectionElementType }); if (addMethod == null) throw new Exception("Failed to get method for reflection."); _addMethod = addMethod; }
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.WriteObject <object>(value, format); // Process the shared resources and external references. writer.WriteSharedResources(); writer.WriteExternalReferences(); // Close the XnaContent element. output.WriteEndElement(); }
public NamespaceAliasHelper(IntermediateSerializer serializer) { _serializer = serializer; }
protected internal virtual void ScanChildren(IntermediateSerializer serializer, ChildCallback callback, T value) { }
protected internal override void ScanChildren(IntermediateSerializer serializer, ChildCallback callback, T[] value) { _listSerializer.ScanChildren(serializer, callback, new List <T>(value)); }
protected internal override void Initialize(IntermediateSerializer serializer) { _listSerializer.Initialize(serializer); }
private bool GetElementInfo(IntermediateSerializer serializer, MemberInfo member, out ElementInfo info) { info = new ElementInfo(); // Are we ignoring this property? if (member.GetCustomAttribute <ContentSerializerIgnoreAttribute>() != null) { return(false); } var prop = member as PropertyInfo; var field = member as FieldInfo; var attrib = member.GetCustomAttribute <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 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 { 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); }
protected internal override void Initialize(IntermediateSerializer serializer) { _itemSerializer = serializer.GetTypeSerializer(typeof(T)); }
protected internal virtual void Initialize(IntermediateSerializer serializer) { }