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 参数传入。

适用于