Partager via


DataContractResolver.TryResolveType Méthode

Définition

Remplacez cette méthode pour mapper un type de contrat de données à un nom et un espace de noms xsi:type pendant la sérialisation.

public:
 abstract bool TryResolveType(Type ^ type, Type ^ declaredType, System::Runtime::Serialization::DataContractResolver ^ knownTypeResolver, [Runtime::InteropServices::Out] System::Xml::XmlDictionaryString ^ % typeName, [Runtime::InteropServices::Out] System::Xml::XmlDictionaryString ^ % typeNamespace);
public abstract bool TryResolveType (Type type, Type declaredType, System.Runtime.Serialization.DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString typeName, out System.Xml.XmlDictionaryString typeNamespace);
public abstract bool TryResolveType (Type type, Type? declaredType, System.Runtime.Serialization.DataContractResolver knownTypeResolver, out System.Xml.XmlDictionaryString? typeName, out System.Xml.XmlDictionaryString? typeNamespace);
abstract member TryResolveType : Type * Type * System.Runtime.Serialization.DataContractResolver * XmlDictionaryString * XmlDictionaryString -> bool
Public MustOverride Function TryResolveType (type As Type, declaredType As Type, knownTypeResolver As DataContractResolver, ByRef typeName As XmlDictionaryString, ByRef typeNamespace As XmlDictionaryString) As Boolean

Paramètres

type
Type

Type à mapper.

declaredType
Type

Type déclaré dans le contrat de données.

knownTypeResolver
DataContractResolver

Programme de résolution de type connu.

typeName
XmlDictionaryString

Nom xsi:type.

typeNamespace
XmlDictionaryString

Espace de noms xsi:type.

Retours

true si le mappage a réussi ; sinon false.

Exemples

L'exemple suivant illustre une implémentation de la méthode TryResolveType.

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

Remarques

Si vous souhaitez utiliser la logique de résolution de type connu à l’intérieur de votre implémentation, une référence au type est passée comme paramètre knownTypeResolver.

S’applique à