DataContractResolver.TryResolveType 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在序列化期间,重写此方法以将数据协定类型映射到 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
参数传入。