private static Dictionary <string, object> GetChildPropertiesAndContent(object element, XmlWriter writer, Type type, ref object contentPropertyValue) { ContentPropertyAttribute attribute = WindowProfileSerializer.GetAttribute <ContentPropertyAttribute>((MemberInfo)type); Dictionary <string, object> dictionary = new Dictionary <string, object>(); foreach (PropertyInfo property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (WindowProfileSerializer.IsPropertySerializable(property)) { object obj = property.GetValue(element, (object[])null); if (!WindowProfileSerializer.IsDefaultValue(property, obj)) { if (WindowProfileSerializer.IsContentProperty(attribute, property)) { contentPropertyValue = obj; } else { TypeConverter converter = TypeDescriptor.GetConverter(obj == null ? property.PropertyType : obj.GetType()); if (converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string))) { writer.WriteStartAttribute(property.Name); WindowProfileSerializer.WriteAttributeValue(converter, obj, writer); writer.WriteEndAttribute(); } else { dictionary[property.Name] = obj; } } } } } return(dictionary); }
private string GetClrNamespace(Type type) { string assemblyName = (string)null; if (!this.namespaceAssembly.TryGetValue(type.Namespace, out assemblyName)) { assemblyName = type.Assembly.GetName().Name; } return(WindowProfileSerializer.GetClrNamespace(type.Namespace, assemblyName)); }
private static bool IsPropertySerializable(PropertyInfo property) { DesignerSerializationVisibilityAttribute attribute = WindowProfileSerializer.GetAttribute <DesignerSerializationVisibilityAttribute>((MemberInfo)property); if (attribute != null && attribute.Visibility == DesignerSerializationVisibility.Hidden || !property.CanRead) { return(false); } if (!property.CanWrite) { return(WindowProfileSerializer.IsSequenceType(property.PropertyType)); } return(true); }
private static bool IsDefaultValue(PropertyInfo property, object value) { DefaultValueAttribute attribute = WindowProfileSerializer.GetAttribute <DefaultValueAttribute>((MemberInfo)property); if (attribute == null) { return(false); } if (object.ReferenceEquals(value, attribute.Value)) { return(true); } if (value != null) { return(value.Equals(attribute.Value)); } return(false); }
public WindowProfile Copy(string newName, WindowProfileSerializer serializer) { using (MemoryStream memoryStream = new MemoryStream()) { XmlReaderSettings settings = new XmlReaderSettings() { CheckCharacters = false }; this.Save((Stream)memoryStream, serializer); memoryStream.Seek(0L, SeekOrigin.Begin); using (XmlReader reader = XmlReader.Create((Stream)memoryStream, settings)) { WindowProfile windowProfile; using (ViewElementFactory.Current.AllowConstruction()) windowProfile = (WindowProfile)XamlReader.Load(reader); windowProfile.Name = newName; return(windowProfile); } } }
public void Serialize(object element, Stream stream) { if (element == null) { throw new ArgumentNullException("element"); } if (WindowProfileSerializer.IsSequenceType(element.GetType())) { throw new ArgumentException("Root serialized element must not be a sequence type", "element"); } XmlWriterSettings settings = new XmlWriterSettings() { CheckCharacters = false, CloseOutput = false, Indent = false, NewLineOnAttributes = false, OmitXmlDeclaration = true, Encoding = Encoding.UTF8 }; using (XmlWriter writer = XmlWriter.Create(stream, settings)) this.SerializeInternal(element, writer, true); }
public void Save(Stream stream, WindowProfileSerializer serializer) { serializer.Serialize((object)this, stream); }
private void SerializeInternal(object element, XmlWriter writer, bool isRootElement) { if (element == null) { return; } ICustomXmlSerializer customXmlSerializer = (ICustomXmlSerializer)null; if (this.Mode == WindowProfileSerializerMode.Custom) { ICustomXmlSerializable customXmlSerializable = element as ICustomXmlSerializable; if (customXmlSerializable != null) { customXmlSerializer = customXmlSerializable.CreateSerializer(); if (customXmlSerializer == null) { return; } } } Type type = element.GetType(); if (this.Mode == WindowProfileSerializerMode.Reflection && WindowProfileSerializer.IsTypeNonSerializable(type)) { return; } if (WindowProfileSerializer.IsSequenceType(type)) { this.SerializeSequence(element as IEnumerable, writer); } else { if (this.GetPrefix(type) != null) { writer.WriteStartElement(this.GetPrefix(type), type.Name, this.GetClrNamespace(type)); } else { writer.WriteStartElement(type.Name, this.GetClrNamespace(type)); } if (isRootElement) { this.WriteNamespaceDeclarations(writer); } object contentPropertyValue = (object)null; IEnumerable <KeyValuePair <string, object> > enumerable; if (customXmlSerializer != null) { customXmlSerializer.WriteXmlAttributes(writer); enumerable = customXmlSerializer.GetNonContentPropertyValues(); contentPropertyValue = customXmlSerializer.Content; } else { enumerable = (IEnumerable <KeyValuePair <string, object> >)WindowProfileSerializer.GetChildPropertiesAndContent(element, writer, type, ref contentPropertyValue); } foreach (KeyValuePair <string, object> keyValuePair in enumerable) { string localName = type.Name + "." + keyValuePair.Key; if (this.GetPrefix(type) != null) { writer.WriteStartElement(this.GetPrefix(type), localName, this.GetClrNamespace(type)); } else { writer.WriteStartElement(localName, this.GetClrNamespace(type)); } this.SerializeInternal(keyValuePair.Value, writer, false); writer.WriteEndElement(); } if (contentPropertyValue != null) { this.SerializeInternal(contentPropertyValue, writer, false); } writer.WriteEndElement(); } }
protected void WriteNamespaceDeclarations(XmlWriter writer) { writer.WriteAttributeString("xmlns", "x", (string)null, "http://schemas.microsoft.com/winfx/2006/xaml"); foreach (string namespaceName in this.namespacePrefix.Keys) { writer.WriteAttributeString("xmlns", this.namespacePrefix[namespaceName], (string)null, WindowProfileSerializer.GetClrNamespace(namespaceName, this.namespaceAssembly[namespaceName])); } }
private static bool IsTypeNonSerializable(Type type) { return(WindowProfileSerializer.GetAttribute <NonXamlSerializedAttribute>((MemberInfo)type) != null); }