Export (0) Print
Expand All

DataContractResolver.TryResolveType Method

Override this method to map a data contract type to an xsi:type name and namespace during serialization.

Namespace:  System.Runtime.Serialization
Assemblies:   System.Runtime.Serialization.Xml (in System.Runtime.Serialization.Xml.dll)
  System.Runtime.Serialization (in System.Runtime.Serialization.dll)

public abstract bool TryResolveType(
	Type type,
	Type declaredType,
	DataContractResolver knownTypeResolver,
	out XmlDictionaryString typeName,
	out XmlDictionaryString typeNamespace


Type: System.Type

The type to map.

Type: System.Type

The type declared in the data contract.

Type: System.Runtime.Serialization.DataContractResolver

The known type resolver.

Type: System.Xml.XmlDictionaryString

The xsi:type name.

Type: System.Xml.XmlDictionaryString

The xsi:type namespace.

Return Value

Type: System.Boolean
true if mapping succeeded; otherwise, false.

If you want to use the known type resolution logic inside your implementation, a reference to it is passed in as the knownTypeResolver parameter.

The following example shows an implementation of the TryResolveType method.

// Used at serialization 
// Maps any Type to a new xsi:type representation 
public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
    string name = type.Name;
    string namesp = type.Namespace;
    typeName = new XmlDictionaryString(XmlDictionary.Empty, name, 0); 
    typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, namesp, 0);
    if (!dictionary.ContainsKey(type.Name))
        dictionary.Add(name, typeName);
    if (!dictionary.ContainsKey(type.Namespace))
        dictionary.Add(namesp, typeNamespace);
    return true;

.NET Framework

Supported in: 4.5.3, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft