共用方式為


DataContractResolver.ResolveName 方法

定義

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

public:
 abstract Type ^ ResolveName(System::String ^ typeName, System::String ^ typeNamespace, Type ^ declaredType, System::Runtime::Serialization::DataContractResolver ^ knownTypeResolver);
public abstract Type ResolveName (string typeName, string typeNamespace, Type declaredType, System.Runtime.Serialization.DataContractResolver knownTypeResolver);
public abstract Type? ResolveName (string typeName, string? typeNamespace, Type? declaredType, System.Runtime.Serialization.DataContractResolver knownTypeResolver);
abstract member ResolveName : string * string * Type * System.Runtime.Serialization.DataContractResolver -> Type
Public MustOverride Function ResolveName (typeName As String, typeNamespace As String, declaredType As Type, knownTypeResolver As DataContractResolver) As Type

參數

typeName
String

要對應的 xsi:type 名稱。

typeNamespace
String

要對應的 xsi:type 命名空間。

declaredType
Type

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

knownTypeResolver
DataContractResolver

已知的型別解析程式。

傳回

xsi:type 名稱和命名空間所對應的型別。

範例

下列範例會示範 ResolveName 方法的實作。

// Used at deserialization
// Allows users to map xsi:type name to any Type
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
{
    XmlDictionaryString tName;
    XmlDictionaryString tNamespace;
    if (dictionary.TryGetValue(typeName, out tName) && dictionary.TryGetValue(typeNamespace, out tNamespace))
    {
        return this.assembly.GetType(tNamespace.Value + "." + tName.Value);
    }
    else
    {
        return null;
    }
}

備註

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

適用於