資料合約序列化程式支援的型別Types Supported by the Data Contract Serializer

Windows Communication Foundation (WCF) 會使用DataContractSerializer為其預設的序列化引擎將資料轉換成 XML,並將 XML 轉換成原來的資料。Windows Communication Foundation (WCF) uses the DataContractSerializer as its default serialization engine to convert data into XML and to convert XML back into data. DataContractSerializer 主要是用來序列化「 資料合約 」(Data Contract) 型別。The DataContractSerializer is designed to serialize data contract types. 但是,它支援其他許多型別,而您可將這些視為擁有隱含資料合約。However, it supports many other types, which can be thought of as having an implicit data contract. 下列是可以序列化的完整型別清單:The following is a complete list of types that can be serialized:

在部分信任模式中使用特定型別的限制Limitations of Using Certain Types in Partial Trust Mode

下列是在部分信任模式案例中使用特定型別的限制清單:The following is a list of limitations when using certain types in partial trust mode scenarios:

  • 若要在部分信任程式碼中,透過 ISerializable 序列化或還原序列化可實作 DataContractSerializer 的型別需要 SerializationFormatterUnmanagedCode 權限。To serialize or deserialize a type that implements ISerializable in partially-trusted code using the DataContractSerializer requires the SerializationFormatter and UnmanagedCode permissions.

  • 當執行中的 WCF 程式碼Indigo2模式、 序列化和還原序列化readonly欄位 (兩者publicprivate) 不支援。When running WCF code in Partial Trust mode, the serialization and deserialization of readonly fields (both public and private) is not supported. 這是因為產生的 IL 無法加以驗證,因此需要較高的權限。This is because the generated IL is unverifiable and therefore requires elevated permissions.

  • 部分信任環境同時支援 DataContractSerializerXmlSerializerBoth the DataContractSerializer and the XmlSerializer are supported in a partial trust environment. 然而, DataContractSerializer 的使用需視下列情況而定:However, use of the DataContractSerializer is subject to the following conditions:

    • 所有可序列化的 [DataContract] 型別必須是公用的。All serializable [DataContract] types must be public.

    • [DataMember] 型別中所有可序列化的 [DataContract] 欄位或屬性必須具有公用和讀/寫性質。All serializable [DataMember] fields or properties in a [DataContract] type must be public and read/write. 序列化和還原序列化readonly時在部分信任的應用程式中執行 WCF 不支援欄位。The serialization and deserialization of readonly fields is not supported when running WCF in a partially-trusted application.

    • 支援 [Serializable]/ISerializable] 程式設計模型。The [Serializable]/ISerializable] programming model is not supported in a partial trust environment.

    • 已知型別必須在程式碼或電腦層級組態 (Machine.config) 中指定。Known types must be specified in code or machine-level configuration (Machine.config). 為了安全起見,已知型別無法在應用程式層級的組態中指定。Known types cannot be specified in application-level configuration for security reasons.

  • 在部分信任環境中,可實作 IObjectReference 的型別會擲回例外狀況,因為 GetRealObject 方法需要安全性權限 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]Types that implement IObjectReference throw an exception in a partially-trusted environment because the GetRealObject method requires the security permission [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)].

其他序列化注意事項Additional Notes on Serialization

下列規則也適用於「資料合約序列化程式」支援的型別:The following rules also apply to types supported by the Data Contract Serializer:

  • 資料合約序列化程式完全支援泛型型別。Generic types are fully supported by the data contract serializer.

  • 資料合約序列化程式完全支援可為 Null 的型別 (Nullable Type)。Nullable types are fully supported by the data contract serializer.

  • 介面型別會被視為 Object 或是集合型別 (在集合介面的案例中)。Interface types are treated either as Object or, in the case of collection interfaces, as collection types.

  • 同時支援結構與類別。Both structures and classes are supported.

  • DataContractSerializer不支援所使用的程式設計模型XmlSerializer和 ASP.NET Web 服務。The DataContractSerializer does not support the programming model used by the XmlSerializer and ASP.NET Web services. 特別是,它不支援 XmlElementAttributeXmlAttributeAttribute之類的屬性。In particular, it does not support attributes like XmlElementAttribute and XmlAttributeAttribute. 若要啟用對此程式設計模型的支援,WCF 必須切換為使用XmlSerializer而不是DataContractSerializerTo enable support for this programming model, WCF must be switched to use the XmlSerializer instead of the DataContractSerializer.

  • DBNull 型別會被特別處理。The DBNull type is treated in a special way. 它是一種單一型別,且在還原序列化時,還原序列化程式會尊重單一限制並將所有 DBNull 參考指向單一執行個體。It is a singleton type, and upon deserialization the deserializer respects the singleton constraint and points all DBNull references to the singleton instance. 由於 DBNull 是一種可序列化型別,它需要 SerializationFormatter 權限。Because DBNull is a serializable type, it demands SerializationFormatter permission.

另請參閱See also