DataContractResolver.TryResolveType Method (Type, Type, DataContractResolver, XmlDictionaryString, XmlDictionaryString)

.NET Framework (current version)
 

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

Namespace:   System.Runtime.Serialization
Assembly:  System.Runtime.Serialization (in System.Runtime.Serialization.dll)

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

Parameters

type
Type: System.Type

The type to map.

declaredType
Type: System.Type

The type declared in the data contract.

knownTypeResolver
Type: System.Runtime.Serialization.DataContractResolver

The known type resolver.

typeName
Type: System.Xml.XmlDictionaryString

The xsi:type name.

typeNamespace
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;
}

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: