共用方式為


DataContractResolver.TryResolveType 方法

定義

覆寫這個方法,以便在序列化期間,將資料合約型別對應至 xsi:type 名稱和命名空間。

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

參數

type
Type

要對應的型別。

declaredType
Type

在資料合約中宣告的型別。

knownTypeResolver
DataContractResolver

已知的型別解析程式。

typeName
XmlDictionaryString

xsi:type 名稱。

typeNamespace
XmlDictionaryString

xsi:type 命名空間。

傳回

如果對應成功,則為 true,否則為 false

範例

下列範例會示範 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;
}

備註

如果您要在實作內使用已知的型別解析邏輯,其參考會當做 knownTypeResolver 參數傳入。

適用於