DataContractResolver DataContractResolver DataContractResolver DataContractResolver Class

定義

提供在序列化及還原序列化期間,以動態方式來回對應型別與 xsi:type 表示的機制。Provides a mechanism for dynamically mapping types to and from xsi:type representations during serialization and deserialization.

public ref class DataContractResolver abstract
public abstract class DataContractResolver
type DataContractResolver = class
Public MustInherit Class DataContractResolver
繼承
DataContractResolverDataContractResolverDataContractResolverDataContractResolver
衍生

範例

下列範例將示範如何從 DataContractResolver 衍生類別。The following example shows how to derive a class from DataContractResolver. 如需實用範例,請參閱 < DataContractResolverFor a working sample, see DataContractResolver.

class MyDataContractResolver : DataContractResolver
{
    private Dictionary<string, XmlDictionaryString> dictionary = new Dictionary<string, XmlDictionaryString>();
    Assembly assembly;

    // Definition of the DataContractResolver
    public MyDataContractResolver(Assembly assembly)
    {
        this.assembly = assembly;
    }

    // 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;
        }
    }
    // 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;
    }
}

備註

開發人員應謹慎正在透過網路傳送的資料。Developers should be careful about what data is being sent over the wire. 您可以使用傳輸或訊息安全性來保護該資料。You can use transport or message security to secure that data. 如需詳細資訊,請參閱安全性For more information, see Security.

警告

只使用DataContractResolver如果您完全確定的序列化資訊。Only use DataContractResolver if you are completely sure of what information is being serialized. 惡意的型別可能會導致非預期的行為。Malicious types can cause unexpected behavior.

建構函式

DataContractResolver() DataContractResolver() DataContractResolver() DataContractResolver()

初始化 DataContractResolver 類別的新執行個體。Initializes a new instance of the DataContractResolver class.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
ResolveName(String, String, Type, DataContractResolver) ResolveName(String, String, Type, DataContractResolver) ResolveName(String, String, Type, DataContractResolver) ResolveName(String, String, Type, DataContractResolver)

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

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
TryResolveType(Type, Type, DataContractResolver, XmlDictionaryString, XmlDictionaryString) TryResolveType(Type, Type, DataContractResolver, XmlDictionaryString, XmlDictionaryString) TryResolveType(Type, Type, DataContractResolver, XmlDictionaryString, XmlDictionaryString) TryResolveType(Type, Type, DataContractResolver, XmlDictionaryString, XmlDictionaryString)

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

適用於