DataContractResolver.TryResolveType(Type, Type, DataContractResolver, XmlDictionaryString, XmlDictionaryString) DataContractResolver.TryResolveType(Type, Type, DataContractResolver, XmlDictionaryString, XmlDictionaryString) DataContractResolver.TryResolveType(Type, Type, DataContractResolver, XmlDictionaryString, XmlDictionaryString) Method

定義

覆寫這個方法,以便在序列化期間,將資料合約型別對應至 xsi:type 名稱和命名空間。Override this method to map a data contract type to an xsi:type name and namespace during serialization.

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 *  *  -> bool

參數

type
Type Type Type

要對應的型別。The type to map.

declaredType
Type Type Type

在資料合約中宣告的型別。The type declared in the data contract.

knownTypeResolver
DataContractResolver DataContractResolver DataContractResolver

已知的型別解析程式。The known type resolver.

typeName
XmlDictionaryString XmlDictionaryString XmlDictionaryString

xsi:type 名稱。The xsi:type name.

typeNamespace
XmlDictionaryString XmlDictionaryString XmlDictionaryString

xsi:type 命名空間。The xsi:type namespace.

傳回

如果對應成功,則為 true,否則為 falsetrue if mapping succeeded; otherwise, false.

範例

下列範例會示範 TryResolveType 方法的實作。The following example shows an implementation of the TryResolveType method.

// 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 參數傳入。If you want to use the known type resolution logic inside your implementation, a reference to it is passed in as the knownTypeResolver parameter.

適用於