Skip to end of metadata
Go to start of metadata

A utility class for serializing objects into Xaml. Does not recognize UI concepts such as DependencyProperties.

Definition

public class XamlSerializer

Summary

The following table summarizes the members exposed in this class.

Public Constructors

XamlSerializer Constructor() Constructs a XamlSerializer.

Protected Properties

AttachedProperties A mapping of target types to discovered attached properties. Derived classes can pre-populate this collection.
DiscoveredTypes A set of the types that have already had their attached properties discovered.
PropertiesToSkip A set of properties to skip when serializing in order to avoid properties that will inevitably throw.
TypeConverters Supplemental TypeConverters (in case a supplied TypeConverter is missing or lacks a ConvertTo implementation).

Constants

XamlNamespace The namespace for Xaml language features (commonly prefixed as "x:")

Public Methods

DiscoverAttachedProperties(Assembly) Searches an assembly for attached properties that can be serialized in Xaml.
DiscoverAttachedProperties(Type) Searches a type for attached properties that can be serialized in Xaml.
RegisterAttachedProperty(MethodInfo, MethodInfo) Registers an attached property for serialization explicitly based upon its getter and setter.
Serialize(object) Serializes an object to Xaml.

Protected Methods

CanWriteObject(object, ISet<object>) Checks to see whether an object can be serialized in Xaml.
CanWriteProperty(PropertyInfo) Checks to see whether a property is writeable (and thus can be serialized).
GetAttachedOrder(AttachedProperty) Gets a string for an attached property used to determine the order in which attached properties will be serialized.
GetAttachedProperties(Type) Gets all of the attached properties that could be applied to the specified type (or its base types or interfaces).
GetContentProperty(object) Gets the ContentProperty for the given object.
GetNamespace(Type, IDictionary<string, string>) Gets the xml namespace for the given type.
GetPrefix(string, IDictionary<string, string>, XmlWriter) Gets an Xml prefix for the given namespace, taking into consideration any existing prefixes in the XmlWriter.
GetPropertyOrder(PropertyInfo) Gets a string for a property used to determine the order in which properties will be serialized.
GetTypeConverter(MemberInfo) Gets the TypeConverter for the given property or attached property.
GetXmlWriter(StringBuilder) Gets an XmlWriter for the given StringBuilder, and can be used to customize formatting settings for the Xaml.
IsInlinable(object, object, MemberInfo, ISet<object>) Determines whether the given property can be written inline (as an attribute) rather than using object-element syntax.
IsInlineAttachedProperty(object, object, MethodInfo, ISet<object>) Determines whether an attached property can be serialized in attribute form rather than using object-element syntax.
IsInlineProperty(object, object, PropertyInfo, ISet<object>) Determines whether a property can be serialized in attribute form rather than using object-element syntax.
IsUnique(object, ISet<object>) Determines whether an object is unique given the items on the stack.
PostSerialize(object, String&) Called after serialization (and can be used for cleanup).
PreSerialize(object, Dictionary<string, string>, ISet<object>) Called before serialization (and can be used for initialization).
ShouldSerialize(object, object, PropertyInfo) Determines whether a property should be serialized (based upon the DefaultAttribute and ShouldSerialize methods).
VisitAfterAttributes(object, XmlWriter, IDictionary<string, string>, ISet<object>) Called after all properties that can be written as attributes (rather than in object-element syntax) are written, but before an object-element content is written. Use this virtual as an opportunity to inject additional attributes before the object is written.
VisitAfterBeginElement(object, XmlWriter, IDictionary<string, string>, ISet<object>) Called immediately after the BeginElement for the object being serialized has been written.
VisitAlternateContent(object, XmlWriter, IDictionary<string, string>, ISet<object>) Called when an object's Content property was not set, allowing special types (e.g. Templates) whose content properties are not discoverable publicly, to be serialized.
VisitAttachedProperty(object, object, string, MethodInfo, MethodInfo, XmlWriter, IDictionary<string, string>, ISet<object>) Serializes an attached property on an object.
VisitBeforeEndElement(object, XmlWriter, IDictionary<string, string>, ISet<object>) Called immediately before the EndElement for the object being serialized is called. Can be used for cleanup.
VisitCollectionContents(IEnumerable, XmlWriter, IDictionary<string, string>, ISet<object>) Serializes the contents of a collection.
VisitDictionaryContents(IDictionary, XmlWriter, IDictionary<string, string>, ISet<object>) Serializes the contents of a dictionary.
VisitKey(object, XmlWriter, IDictionary<string, string>, ISet<object>) Serializes a key for an object (usually as "x:Key"). The writer will be in attribute mode.
VisitObject(object, XmlWriter, IDictionary<string, string>, ISet<object>, bool, object) Serializes an object into Xaml.
VisitProperty(object, object, PropertyInfo, bool, XmlWriter, IDictionary<string, string>, ISet<object>) Serializes a property.
VisitRootAttribute(object, XmlWriter, IDictionary<string, string>, ISet<object>) Called during attribute serialization on the root object. Can be used for global namespace declaration.

Public Constructors

public XamlSerializer()

Constructs a XamlSerializer.

Protected Properties

protected Dictionary<Type, List<AttachedProperty>> AttachedProperties { get; set; }

A mapping of target types to discovered attached properties. Derived classes can pre-populate this collection.

protected HashSet<Type> DiscoveredTypes { get; set; }

A set of the types that have already had their attached properties discovered.

protected HashSet<PropertyInfo> PropertiesToSkip { get; set; }

A set of properties to skip when serializing in order to avoid properties that will inevitably throw.

protected IDictionary<Type, TypeConverter> TypeConverters { get; set; }

Supplemental TypeConverters (in case a supplied TypeConverter is missing or lacks a ConvertTo implementation).

Constants

public const string XamlNamespace

The namespace for Xaml language features (commonly prefixed as "x:")

Public Methods

public void DiscoverAttachedProperties(Assembly asm)

Searches an assembly for attached properties that can be serialized in Xaml.

Parameters

asm The assembly to search.

public void DiscoverAttachedProperties(Type type)

Searches a type for attached properties that can be serialized in Xaml.

Parameters

type The type to search.

public bool RegisterAttachedProperty(MethodInfo getter, MethodInfo setter)

Registers an attached property for serialization explicitly based upon its getter and setter.

Parameters

getter The getter for the attached property.
setter The setter for the attached property.

Return Types

true if the attached property was valid and registered, otherwise false.

public string Serialize(object obj)

Serializes an object to Xaml.

Parameters

obj The object to serialize.

Return Types

A Xaml string representing the object.

Protected Methods

protected bool CanWriteObject(object obj, ISet<object> cycleCheckObjects)

Checks to see whether an object can be serialized in Xaml.

Parameters

obj The object to be serialized.
cycleCheckObjects The set of objects currently on the stack (used to avoid cycles).

Return Types

true if the object can be serialized, false otherwise.

protected bool CanWriteProperty(PropertyInfo p)

Checks to see whether a property is writeable (and thus can be serialized).

Parameters

p The property to check.

Return Types

true if the property can be written, false otherwise.

protected string GetAttachedOrder(AttachedProperty ap)

Gets a string for an attached property used to determine the order in which attached properties will be serialized.

Parameters

ap The attached property being serialized.

Return Types

A sortable string representation for the attached property.

protected IEnumerable<AttachedProperty> GetAttachedProperties(Type t)

Gets all of the attached properties that could be applied to the specified type (or its base types or interfaces).

Parameters

t The type for which attached properties should be retrieved.

Return Types

A collection of attached properties for the given type.

protected PropertyInfo GetContentProperty(object obj)

Gets the ContentProperty for the given object.

Parameters

obj The object being serialized.

Return Types

The ContentProperty for the given object, or null if one is not specified.

protected string GetNamespace(Type t, IDictionary<string, string> prefixMappings)

Gets the xml namespace for the given type.

Parameters

t The type whose namespace must be retrieved.
prefixMappings A mapping of xml namespaces to prefixes.

Return Types

The xml namespace for the type.

protected string GetPrefix(string ns, IDictionary<string, string> prefixMappings, XmlWriter writer)

Gets an Xml prefix for the given namespace, taking into consideration any existing prefixes in the XmlWriter.

Parameters

ns The namespace for which a namespace must be assigned.
prefixMappings Existing prefix mappings.
writer The writer which will consume the prefix.

Return Types

A prefix for the given namespace.

protected string GetPropertyOrder(PropertyInfo prop)

Gets a string for a property used to determine the order in which properties will be serialized.

Parameters

prop The property being serialized.

Return Types

A sortable string representation for the property.

protected TypeConverter GetTypeConverter(MemberInfo inf)

Gets the TypeConverter for the given property or attached property.

Parameters

inf The property or getter for the attached property.

Return Types

The TypeConverter to be used for serialization if one exists, otherwise null.

protected XmlWriter GetXmlWriter(StringBuilder output)

Gets an XmlWriter for the given StringBuilder, and can be used to customize formatting settings for the Xaml.

Parameters

output The StringBuilder into which the writer will emit Xaml.

Return Types

An XmlWriter to be used for serialization.

protected bool IsInlinable(object obj, object propValue, MemberInfo inf, ISet<object> cycleCheckObjects)

Determines whether the given property can be written inline (as an attribute) rather than using object-element syntax.

Parameters

obj The object on which the property is being set.
propValue The value of the property being set.
inf The identifier for the property being set (a PropertyInfo for a property, and the getter MethodInfo for an attached property).
cycleCheckObjects The set of objects on the stack (for cycle detection).

protected bool IsInlineAttachedProperty(object obj, object propValue, MethodInfo meth, ISet<object> cycleCheckObjects)

Determines whether an attached property can be serialized in attribute form rather than using object-element syntax.

Parameters

obj The object on which the attached property is set.
propValue The value of the attached property.
meth The getter for the attached property.
cycleCheckObjects The set of objects on the stack (for cycle detection).

Return Types

true if the attached property can be written as an attribute, false otherwise.

protected bool IsInlineProperty(object obj, object propValue, PropertyInfo prop, ISet<object> cycleCheckObjects)

Determines whether a property can be serialized in attribute form rather than using object-element syntax.

Parameters

obj The object on which the property is set.
propValue The value of the property.
prop The property to serialize.
cycleCheckObjects The set of objects on the stack (for cycle detection).

Return Types

true if the property can be written as an attribute, false otherwise.

protected bool IsUnique(object obj, ISet<object> cycleCheckObjects)

Determines whether an object is unique given the items on the stack.

Parameters

obj The object to check.
cycleCheckObjects The set of objects on the stack (for cycle detection).

Return Types

true if the object is unique, false otherwise.

protected void PostSerialize(object obj, String& str)

Called after serialization (and can be used for cleanup).

Parameters

obj The object being serialized.
str The serialized Xaml, which can be modified for cleanup during this method.

protected void PreSerialize(object obj, Dictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Called before serialization (and can be used for initialization).

Parameters

obj The object being serialized.
prefixMappings The initial dictionary of prefixes, which can be primed for custom serializers.
cycleCheckObjects The set of objects on the stack, which can be primed if certain objects must be avoided.

protected bool ShouldSerialize(object obj, object propValue, PropertyInfo prop)

Determines whether a property should be serialized (based upon the DefaultAttribute and ShouldSerialize methods).

Parameters

obj The object being serialized.
propValue The value of the property being serialized.
prop The property being serialized.

Return Types

true if the property should be serialized, false otherwise.

protected void VisitAfterAttributes(object obj, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Called after all properties that can be written as attributes (rather than in object-element syntax) are written, but before an object-element content is written. Use this virtual as an opportunity to inject additional attributes before the object is written.

Parameters

obj The object being serialized.
writer The writer being used for serialization.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects on the stack (for cycle detection).

protected void VisitAfterBeginElement(object obj, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Called immediately after the BeginElement for the object being serialized has been written.

Parameters

obj The object being serialized.
writer The writer being used for serialization.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects on the stack (for cycle detection).

protected void VisitAlternateContent(object obj, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Called when an object's Content property was not set, allowing special types (e.g. Templates) whose content properties are not discoverable publicly, to be serialized.

Parameters

obj The object being serialized.
writer The writer being used for serialization.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects on the stack (for cycle detection).

protected void VisitAttachedProperty(object obj, object propValue, string propertyName, MethodInfo getter, MethodInfo setter, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Serializes an attached property on an object.

Parameters

obj The object on which the attached property is set.
propValue The value of the attached property.
propertyName The name of the attached property.
getter The getter method for the attached property.
setter The setter method for the attached property.
writer The writer being used to serialize the object.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects currently on the stack (for cycle detection).

protected void VisitBeforeEndElement(object obj, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Called immediately before the EndElement for the object being serialized is called. Can be used for cleanup.

Parameters

obj The object being serialized.
writer The writer being used for serialization.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects on the stack (for cycle detection).

protected void VisitCollectionContents(IEnumerable collection, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Serializes the contents of a collection.

Parameters

collection The collection being serialized.
writer The writer being used for serialization.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects on the stack (for cycle detection).

protected void VisitDictionaryContents(IDictionary dict, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Serializes the contents of a dictionary.

Parameters

dict The dictionary being serialized.
writer The writer being used for serialization.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects on the stack (for cycle detection).

protected void VisitKey(object key, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Serializes a key for an object (usually as "x:Key"). The writer will be in attribute mode.

Parameters

key The key to serialize.
writer The writer being used for serialization.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects on the stack (for cycle detection).

protected void VisitObject(object obj, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects, bool isRoot, object key)

Serializes an object into Xaml.

Parameters

obj The object to serialize.
writer The writer being used for serialization.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects on the stack (for cycle detection)
isRoot A value indicating whether this object is the root of the document.
key A value that represents the key for this object, or null if the object has no key (and hence is not in a dictionary).

protected void VisitProperty(object obj, object propValue, PropertyInfo prop, bool isContentProperty, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Serializes a property.

Parameters

obj The object on which the property is set.
propValue The value of the property.
prop The property being set.
isContentProperty A value indicating that the property is the ContentProperty for the object, and thus no property elements need to be generated.
writer The writer being used for serialization.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects on the stack (for cycle detection).

protected void VisitRootAttribute(object obj, XmlWriter writer, IDictionary<string, string> prefixMappings, ISet<object> cycleCheckObjects)

Called during attribute serialization on the root object. Can be used for global namespace declaration.

Parameters

obj The root object.
writer The writer being used for serialization.
prefixMappings A mapping of xml namespaces to prefixes.
cycleCheckObjects The set of objects on the stack (for cycle detection).
  • No labels