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);
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 該參考。