共用方式為


DataContractSerializer 建構函式

定義

初始化 DataContractSerializer 類別的新執行個體。

多載

DataContractSerializer(Type)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單、要序列化之圖形項目的最大數、要略過非預期資料的參數、是否要使用非標準的 XML 建構來保存圖形中的物件參考資料、自訂序列化的代理、XmlDictionaryString 的參數 (會指定 XML 項目和包含內容的命名空間),以及在執行階段對應 xsi:type 宣告的替代方式。

DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單、要序列化之圖形項目的最大數、要略過非預期資料的參數、是否要使用非標準的 XML 建構來保存圖形中的物件參考資料、自訂序列化的代理、XML 項目和包含內容的命名空間,以及在執行階段對應 xsi:type 宣告的替代方式。

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單,要序列化之圖形項目的最大數,要略過非預期資料的參數,是否要使用非標準的 XML 建構來保存圖形中的物件參考資料,自訂序列化的代理,以及會指定其中含有內容之 XML 項目和命名空間的 XmlDictionaryString 參數。

DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單、要序列化之圖形項目的最大數、要略過非預期資料的參數、是否要使用非標準的 XML 建構來保存圖形中的物件參考資料、自訂序列化的代理,以及含有內容的 XML 項目和命名空間。

DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單,要序列化之圖形項目的最大數,要略過非預期資料的參數,是否要使用非標準的 XML 建構來保存圖形中的物件參考資料,以及自訂序列化的代理。

DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單、要序列化之圖形項目的最大數、要略過非預期資料的參數、是否要使用非標準的 XML 建構來保存圖形中的物件參考資料、自訂序列化的代理,以及在執行階段對應 xsi:type 宣告的替代方式。

DataContractSerializer(Type, String, String, IEnumerable<Type>)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會透過兩個字串參數來指定根 XML 項目和命名空間,以及可能存在於物件圖形內之已知型別的清單。

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)

初始化 DataContractSerializer 類別的新執行個體,以便序列化或還原序列化所指定型別的物件 (該型別會使用透過型別 XmlDictionaryString 之參數所指定的 XML 根項目和命名空間)。

DataContractSerializer(Type, String, String)

初始化 DataContractSerializer 類別的新執行個體,以便序列化或還原序列化已指定型別的物件 (該型別會使用提供的 XML 根項目和命名空間)。

DataContractSerializer(Type, DataContractSerializerSettings)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別及設定的物件。

DataContractSerializer(Type, IEnumerable<Type>)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件,以及可能存在物件圖形中的已知型別集合。

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會透過兩個 XmlDictionaryString 參數來指定根 XML 項目和命名空間,以及可能存在於物件圖形內之已知型別的清單。

DataContractSerializer(Type)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。

public:
 DataContractSerializer(Type ^ type);
public DataContractSerializer (Type type);
new System.Runtime.Serialization.DataContractSerializer : Type -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type)

參數

type
Type

已序列化或還原序列化之執行個體的型別。

範例

下列範例會建立 DataContractSerializer 的執行個體,該執行個體會指定要序列化或還原序列化的型別。

public static void Constructor1()
{
    // Create an instance of the DataContractSerializer.
    DataContractSerializer ser =
        new DataContractSerializer(typeof(Person));
    // Other code not shown.
}
 Public Shared Sub Constructor1() 
     ' Create an instance of the DataContractSerializer.
     Dim ser As New DataContractSerializer(GetType(Person))

     ' Other code not shown.    
 End Sub

適用於

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單、要序列化之圖形項目的最大數、要略過非預期資料的參數、是否要使用非標準的 XML 建構來保存圖形中的物件參考資料、自訂序列化的代理、XmlDictionaryString 的參數 (會指定 XML 項目和包含內容的命名空間),以及在執行階段對應 xsi:type 宣告的替代方式。

public:
 DataContractSerializer(Type ^ type, System::Xml::XmlDictionaryString ^ rootName, System::Xml::XmlDictionaryString ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate, System::Runtime::Serialization::DataContractResolver ^ dataContractResolver);
public DataContractSerializer (Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate, System.Runtime.Serialization.DataContractResolver dataContractResolver);
new System.Runtime.Serialization.DataContractSerializer : Type * System.Xml.XmlDictionaryString * System.Xml.XmlDictionaryString * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate * System.Runtime.Serialization.DataContractResolver -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As XmlDictionaryString, rootNamespace As XmlDictionaryString, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate, dataContractResolver As DataContractResolver)

參數

type
Type

已序列化或還原序列化之執行個體的型別。

rootName
XmlDictionaryString

內含要序列化或還原序列化之內容的 XML 項目。

rootNamespace
XmlDictionaryString

內含要序列化或還原序列化之內容的 XML 項目命名空間。

knownTypes
IEnumerable<Type>

IEnumerable<T>Type,其中包含可能存在物件圖形中的已知型別。

maxItemsInObjectGraph
Int32

圖形中要序列化或還原序列化的最大項目數。

ignoreExtensionDataObject
Boolean

如果要在序列化及還原序列化時略過型別延伸所提供的資料,則為 true,否則為 false

preserveObjectReferences
Boolean

如果要使用非標準的 XML 建構來保留物件參考資料,則為 true,否則為 false

dataContractSurrogate
IDataContractSurrogate

IDataContractSurrogate 的實作,用來自訂序列化程序。

dataContractResolver
DataContractResolver

DataContractResolver 的實作,用來將 xsi:type 宣告對應至資料合約型別。

適用於

DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單、要序列化之圖形項目的最大數、要略過非預期資料的參數、是否要使用非標準的 XML 建構來保存圖形中的物件參考資料、自訂序列化的代理、XML 項目和包含內容的命名空間,以及在執行階段對應 xsi:type 宣告的替代方式。

public:
 DataContractSerializer(Type ^ type, System::String ^ rootName, System::String ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate, System::Runtime::Serialization::DataContractResolver ^ dataContractResolver);
public DataContractSerializer (Type type, string rootName, string rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate, System.Runtime.Serialization.DataContractResolver dataContractResolver);
new System.Runtime.Serialization.DataContractSerializer : Type * string * string * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate * System.Runtime.Serialization.DataContractResolver -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As String, rootNamespace As String, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate, dataContractResolver As DataContractResolver)

參數

type
Type

已序列化或還原序列化之執行個體的型別。

rootName
String

內含要序列化或還原序列化之內容的 XML 項目。

rootNamespace
String

內含要序列化或還原序列化之內容的 XML 項目命名空間。

knownTypes
IEnumerable<Type>

IEnumerable<T>Type,其中包含可能存在物件圖形中的已知型別。

maxItemsInObjectGraph
Int32

圖形中要序列化或還原序列化的最大項目數。

ignoreExtensionDataObject
Boolean

如果要在序列化及還原序列化時略過型別延伸所提供的資料,則為 true,否則為 false

preserveObjectReferences
Boolean

如果要使用非標準的 XML 建構來保留物件參考資料,則為 true,否則為 false

dataContractSurrogate
IDataContractSurrogate

IDataContractSurrogate 的實作,用來自訂序列化程序。

dataContractResolver
DataContractResolver

DataContractResolver 的實作,用來將 xsi:type 宣告對應至資料合約型別。

適用於

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單,要序列化之圖形項目的最大數,要略過非預期資料的參數,是否要使用非標準的 XML 建構來保存圖形中的物件參考資料,自訂序列化的代理,以及會指定其中含有內容之 XML 項目和命名空間的 XmlDictionaryString 參數。

public:
 DataContractSerializer(Type ^ type, System::Xml::XmlDictionaryString ^ rootName, System::Xml::XmlDictionaryString ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate);
public DataContractSerializer (Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate);
new System.Runtime.Serialization.DataContractSerializer : Type * System.Xml.XmlDictionaryString * System.Xml.XmlDictionaryString * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As XmlDictionaryString, rootNamespace As XmlDictionaryString, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate)

參數

type
Type

已序列化或還原序列化之執行個體的型別。

rootName
XmlDictionaryString

指定內含要序列化或還原序列化之 XML 項目的 XmlDictionaryString

rootNamespace
XmlDictionaryString

XmlDictionaryString,會指定根的 XML 命名空間。

knownTypes
IEnumerable<Type>

IEnumerable<T>Type,其中包含可能存在物件圖形中的已知型別。

maxItemsInObjectGraph
Int32

圖形中要序列化或還原序列化的最大項目數。

ignoreExtensionDataObject
Boolean

如果要在序列化及還原序列化時略過型別延伸所提供的資料,則為 true,否則為 false

preserveObjectReferences
Boolean

如果要使用非標準的 XML 建構來保留物件參考資料,則為 true,否則為 false

dataContractSurrogate
IDataContractSurrogate

IDataContractSurrogate 的實作,用來自訂序列化程序。

例外狀況

超過最大值的項目數。

範例

下列範例會建立會指定要序列化或還原序列化之型別的 DataContractSerializer 的執行個體,並建立會包含要在還原序列化期間使用之 XML 項目、命名空間 (相同於 XmlDictionaryString 參數的處理方式) 和 IEnumerable<T> 的執行個體。 程序代碼也會將 ignoreExtensionDataObjectpreserveObjectReferences 參數設定為 true,並指定 介面的 IDataContractSurrogate 實作,以處理未 DataContractAttribute 套用屬性的舊版類型 (型別) 。 如需詳細資訊,請參閱 IDataContractSurrogate 文件。

public static void Constructor9()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    // Create an XmlDictionary and add values to it.
    XmlDictionary d = new XmlDictionary();
    XmlDictionaryString name_value = d.Add("Customer");
    XmlDictionaryString ns_value = d.Add("http://www.contoso.com");

    // Create an instance of a class that
    // implements the IDataContractSurrogate interface.
    // The implementation code is not shown here.
    DCSurrogate mySurrogate = new DCSurrogate();

    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        name_value,
        ns_value,
        knownTypeList,
        64 * 1024,
        true,
        true,
        mySurrogate);

    // Other code not shown.
}
Public Shared Sub Constructor9() 

    ' Create a generic List of types and add the known types
    ' to the collection.
    Dim knownTypeList As New List(Of Type)
    knownTypeList.Add(GetType(PurchaseOrder))
    knownTypeList.Add(GetType(PurchaseOrderV3))
    
    ' Create an XmlDictionary and add values to it.
    Dim d As New XmlDictionary()
    Dim name_value As XmlDictionaryString = d.Add("Customer")
    Dim ns_value As XmlDictionaryString = d.Add("http://www.contoso.com")
    
    ' Create an instance of a class that 
    ' implements the IDataContractSurrogate interface.
    ' The implementation code is not shown here.
    Dim mySurrogate As New DCSurrogate()
    
    Dim ser As New DataContractSerializer(GetType(Person), _
    name_value, _
    ns_value, _
    knownTypeList, _
    64 * 1064, _
    True, _
    True, _
    mySurrogate)

    ' Other code not shown.    

End Sub

另請參閱

適用於

DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單、要序列化之圖形項目的最大數、要略過非預期資料的參數、是否要使用非標準的 XML 建構來保存圖形中的物件參考資料、自訂序列化的代理,以及含有內容的 XML 項目和命名空間。

public:
 DataContractSerializer(Type ^ type, System::String ^ rootName, System::String ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate);
public DataContractSerializer (Type type, string rootName, string rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate);
new System.Runtime.Serialization.DataContractSerializer : Type * string * string * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As String, rootNamespace As String, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate)

參數

type
Type

已序列化或還原序列化之執行個體的型別。

rootName
String

內含要序列化或還原序列化之內容的 XML 項目。

rootNamespace
String

內含要序列化或還原序列化之內容的 XML 項目命名空間。

knownTypes
IEnumerable<Type>

IEnumerable<T>Type,其中包含可能存在物件圖形中的已知型別。

maxItemsInObjectGraph
Int32

圖形中要序列化或還原序列化的最大項目數。

ignoreExtensionDataObject
Boolean

如果要在序列化及還原序列化時略過型別延伸所提供的資料,則為 true,否則為 false

preserveObjectReferences
Boolean

如果要使用非標準的 XML 建構來保留物件參考資料,則為 true,否則為 false

dataContractSurrogate
IDataContractSurrogate

IDataContractSurrogate 的實作,用來自訂序列化程序。

例外狀況

超過最大值的項目數。

範例

下列範例會建立會指定要序列化或還原序列化之型別的 DataContractSerializer 的執行個體,並建立會包含要在還原序列化期間使用之 XML 項目、命名空間和 IEnumerable<T> 的執行個體。 程序代碼也會將 ignoreExtensionDataObjectpreserveObjectReferences 參數設定為 true,並指定 介面的 IDataContractSurrogate 實作,以處理未 DataContractAttribute 套用屬性的舊版類型 (型別) 。 如需詳細資訊,請參閱 IDataContractSurrogate 文件。

public static void Constructor8()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    // Create an instance of a class that
    // implements the IDataContractSurrogate interface.
    // The implementation code is not shown here.
    DCSurrogate mySurrogate = new DCSurrogate();

    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        "Customer",
        @"http://www.contoso.com",
        knownTypeList,
        64 * 1024,
        true,
        true,
        mySurrogate);

    // Other code not shown.
}
Public Shared Sub Constructor8() 

    ' Create a generic List of types and add the known types
    ' to the collection.
    Dim knownTypeList As New List(Of Type)
    knownTypeList.Add(GetType(PurchaseOrder))
    knownTypeList.Add(GetType(PurchaseOrderV3))

    ' Create an instance of a class that 
    ' implements the IDataContractSurrogate interface.
    ' The implementation code is not shown here.
    Dim mySurrogate As New DCSurrogate()

    Dim ser As New DataContractSerializer(GetType(Person), _
    "Customer", _
    "http://www.contoso.com", _
    knownTypeList, _
    64 * 1064, _
    True, _
    True, _
    mySurrogate)

    ' Other code not shown.
End Sub

另請參閱

適用於

DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單,要序列化之圖形項目的最大數,要略過非預期資料的參數,是否要使用非標準的 XML 建構來保存圖形中的物件參考資料,以及自訂序列化的代理。

public:
 DataContractSerializer(Type ^ type, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate);
public DataContractSerializer (Type type, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate);
new System.Runtime.Serialization.DataContractSerializer : Type * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate)

參數

type
Type

已序列化或還原序列化之執行個體的型別。

knownTypes
IEnumerable<Type>

IEnumerable<T>Type,其中包含可能存在物件圖形中的已知型別。

maxItemsInObjectGraph
Int32

圖形中要序列化或還原序列化的最大項目數。 預設值是 Int32.MaxValue 屬性所傳回的值。

ignoreExtensionDataObject
Boolean

如果要在序列化及還原序列化時略過型別延伸所提供的資料,則為 true,否則為 false

preserveObjectReferences
Boolean

如果要使用非標準的 XML 建構來保留物件參考資料,則為 true,否則為 false

dataContractSurrogate
IDataContractSurrogate

IDataContractSurrogate 的實作,用來自訂序列化程序。

例外狀況

超過最大值的項目數。

範例

下列範例會建立會指定要序列化或還原序列化之型別的 DataContractSerializer 的執行個體,並建立會包含要在序列化或還原序列化期間使用之已知型別的 IEnumerable<T> 執行個體。 程序代碼也會將 ignoreExtensionDataObjectpreserveObjectReferences 參數設定為 true,並指定 介面的 IDataContractSurrogate 實作,以處理未 DataContractAttribute 套用屬性的舊版類型 (型別) 。 如需詳細資訊,請參閱 IDataContractSurrogate 文件。

public static void Constructor7()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    // Create an instance of a class that
    // implements the IDataContractSurrogate interface.
    // The implementation code is not shown here.
    DCSurrogate mySurrogate = new DCSurrogate();

    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        knownTypeList,
        64 * 1024,
        true,
        true,
        mySurrogate);
    // Other code not shown.
}
Public Shared Sub Constructor7() 

    ' Create a generic List of types and add the known types
    ' to the collection.
    Dim knownTypeList As New List(Of Type)
    knownTypeList.Add(GetType(PurchaseOrder))
    knownTypeList.Add(GetType(PurchaseOrderV3))

    ' Create an instance of a class that 
    ' implements the IDataContractSurrogate interface.
    ' The implementation code is not shown here.
    Dim mySurrogate As New DCSurrogate()

    Dim ser As New DataContractSerializer(GetType(Person), _
    knownTypeList, _
    64 * 1064, _
    True, _
    True, _
     mySurrogate)

    ' Other code not shown.
End Sub

另請參閱

適用於

DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會指定可能存在物件圖形中之已知型別的清單、要序列化之圖形項目的最大數、要略過非預期資料的參數、是否要使用非標準的 XML 建構來保存圖形中的物件參考資料、自訂序列化的代理,以及在執行階段對應 xsi:type 宣告的替代方式。

public:
 DataContractSerializer(Type ^ type, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate, System::Runtime::Serialization::DataContractResolver ^ dataContractResolver);
public DataContractSerializer (Type type, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate, System.Runtime.Serialization.DataContractResolver dataContractResolver);
new System.Runtime.Serialization.DataContractSerializer : Type * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate * System.Runtime.Serialization.DataContractResolver -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate, dataContractResolver As DataContractResolver)

參數

type
Type

已序列化或還原序列化之執行個體的型別。

knownTypes
IEnumerable<Type>

IEnumerable<T>Type,其中包含可能存在物件圖形中的已知型別。

maxItemsInObjectGraph
Int32

圖形中要序列化或還原序列化的最大項目數。 預設值是 Int32.MaxValue 屬性所傳回的值。

ignoreExtensionDataObject
Boolean

如果要在序列化及還原序列化時略過型別延伸所提供的資料,則為 true,否則為 false

preserveObjectReferences
Boolean

如果要使用非標準的 XML 建構來保留物件參考資料,則為 true,否則為 false

dataContractSurrogate
IDataContractSurrogate

IDataContractSurrogate 的實作,用來自訂序列化程序。

dataContractResolver
DataContractResolver

DataContractResolver 的實作,用來將 xsi:type 宣告對應至資料合約型別。

備註

警告

此方法不適用於 Windows 市集應用程式。

適用於

DataContractSerializer(Type, String, String, IEnumerable<Type>)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會透過兩個字串參數來指定根 XML 項目和命名空間,以及可能存在於物件圖形內之已知型別的清單。

public:
 DataContractSerializer(Type ^ type, System::String ^ rootName, System::String ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes);
public DataContractSerializer (Type type, string rootName, string rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes);
public DataContractSerializer (Type type, string rootName, string rootNamespace, System.Collections.Generic.IEnumerable<Type>? knownTypes);
new System.Runtime.Serialization.DataContractSerializer : Type * string * string * seq<Type> -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As String, rootNamespace As String, knownTypes As IEnumerable(Of Type))

參數

type
Type

已序列化或還原序列化之執行個體的型別。

rootName
String

內容的根項目名稱。

rootNamespace
String

根項目的命名空間。

knownTypes
IEnumerable<Type>

IEnumerable<T>Type,其中包含可能存在物件圖形中的型別。

範例

下列範例會建立 DataContractSerializer 的執行個體,該執行個體會指定要序列化或還原序列化的型別,以及要在其中讀取或將 XML 文件寫入其中的 XML 名稱和命名空間。 此程式碼也會建立 IEnumerable<T> 的執行個體,以包含在序列化或還原序列化期間所使用的已知型別。

public static void Constructor5()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        "Customer",
        @"http://www.contoso.com",
        knownTypeList);

    // Other code not shown.
}
Public Shared Sub Constructor5() 

    ' Create a generic List of types and add the known types
    ' to the collection.
    Dim knownTypeList As New List(Of Type)
    knownTypeList.Add(GetType(PurchaseOrder))
    knownTypeList.Add(GetType(PurchaseOrderV3))
    
    Dim ser As New DataContractSerializer(GetType(Person), _
    "Customer", _
    "http://www.contoso.com", _
    knownTypeList)

    ' Other code not shown.

End Sub

另請參閱

適用於

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs

初始化 DataContractSerializer 類別的新執行個體,以便序列化或還原序列化所指定型別的物件 (該型別會使用透過型別 XmlDictionaryString 之參數所指定的 XML 根項目和命名空間)。

public:
 DataContractSerializer(Type ^ type, System::Xml::XmlDictionaryString ^ rootName, System::Xml::XmlDictionaryString ^ rootNamespace);
public DataContractSerializer (Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace);
new System.Runtime.Serialization.DataContractSerializer : Type * System.Xml.XmlDictionaryString * System.Xml.XmlDictionaryString -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As XmlDictionaryString, rootNamespace As XmlDictionaryString)

參數

type
Type

已序列化或還原序列化之執行個體的型別。

rootName
XmlDictionaryString

XmlDictionaryString,其中包含內容的根項目名稱。

rootNamespace
XmlDictionaryString

XmlDictionaryString,其中包含根項目的命名空間。

範例

下列範例會建立 DataContractSerializer 的執行個體,該執行個體會指定要序列化或還原序列化的型別,以及要在其中讀取或將 XML 文件寫入其中的 XML 名稱和命名空間 (相同於 XmlDictionaryString 物件的處理方式)。

public static void Constructor4()
{
    // Create an instance of the DataContractSerializer
    // specifying the type, and name and
    // namespace as XmlDictionaryString objects.

    // Create an XmlDictionary and add values to it.
    XmlDictionary d = new XmlDictionary();
    XmlDictionaryString name_value = d.Add("Customer");
    XmlDictionaryString ns_value = d.Add("http://www.contoso.com");

    // Create the serializer.
    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        name_value,
        ns_value);
    // Other code not shown.
}
Public Shared Sub Constructor4() 
    ' Create an instance of the DataContractSerializer
    ' specifying the type, and name and 
    ' namespace as XmlDictionaryString objects.
    ' Create an XmlDictionary and add values to it.
    Dim d As New XmlDictionary()
    Dim name_value As XmlDictionaryString = d.Add("Customer")
    Dim ns_value As XmlDictionaryString = d.Add("http://www.contoso.com")
    
    ' Create the serializer.
    Dim ser As New DataContractSerializer(GetType(Person), _
    name_value, _
    ns_value)

    ' Other code not shown.
End Sub

適用於

DataContractSerializer(Type, String, String)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs

初始化 DataContractSerializer 類別的新執行個體,以便序列化或還原序列化已指定型別的物件 (該型別會使用提供的 XML 根項目和命名空間)。

public:
 DataContractSerializer(Type ^ type, System::String ^ rootName, System::String ^ rootNamespace);
public DataContractSerializer (Type type, string rootName, string rootNamespace);
new System.Runtime.Serialization.DataContractSerializer : Type * string * string -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As String, rootNamespace As String)

參數

type
Type

已序列化或還原序列化之執行個體的型別。

rootName
String

內含要序列化或還原序列化之內容的 XML 項目名稱。

rootNamespace
String

內含要序列化或還原序列化之內容的 XML 項目命名空間。

範例

下列範例會建立 DataContractSerializer 的執行個體,該執行個體會指定要序列化或還原序列化的型別,以及要在其中讀取或將 XML 文件寫入其中的 XML 名稱和命名空間。

public static void Constructor3()
{
    // Create an instance of the DataContractSerializer
    // specifying the type, and name and
    // namespace as strings.
    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        "Customer",
        "http://www.contoso.com");

    // Other code not shown.
}
Public Shared Sub Constructor3() 
    ' Create an instance of the DataContractSerializer
    ' specifying the type, and name and 
    ' namespace as strings.
    Dim ser As New DataContractSerializer(GetType(Person), _
    "Customer", _
    "http://www.contoso.com")

    ' Other code not shown.
End Sub

適用於

DataContractSerializer(Type, DataContractSerializerSettings)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別及設定的物件。

public:
 DataContractSerializer(Type ^ type, System::Runtime::Serialization::DataContractSerializerSettings ^ settings);
public DataContractSerializer (Type type, System.Runtime.Serialization.DataContractSerializerSettings settings);
public DataContractSerializer (Type type, System.Runtime.Serialization.DataContractSerializerSettings? settings);
new System.Runtime.Serialization.DataContractSerializer : Type * System.Runtime.Serialization.DataContractSerializerSettings -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, settings As DataContractSerializerSettings)

參數

type
Type

要序列化或還原序列化之執行個體的型別。

settings
DataContractSerializerSettings

序列化程式設定。

適用於

DataContractSerializer(Type, IEnumerable<Type>)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件,以及可能存在物件圖形中的已知型別集合。

public:
 DataContractSerializer(Type ^ type, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes);
public DataContractSerializer (Type type, System.Collections.Generic.IEnumerable<Type> knownTypes);
public DataContractSerializer (Type type, System.Collections.Generic.IEnumerable<Type>? knownTypes);
new System.Runtime.Serialization.DataContractSerializer : Type * seq<Type> -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, knownTypes As IEnumerable(Of Type))

參數

type
Type

已序列化或還原序列化之執行個體的型別。

knownTypes
IEnumerable<Type>

IEnumerable<T>Type,其中包含可能存在物件圖形中的型別。

範例

下列範例會建立 DataContractSerializer 的執行個體,該執行個體會指定要序列化或還原序列化的型別,以及可用於物件圖形中的已知型別集合。

public static void Constructor2()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    // Create a DatatContractSerializer with the collection.
    DataContractSerializer ser2 = new DataContractSerializer(
        typeof(Orders), knownTypeList);

    // Other code not shown.
}
 Public Shared Sub Constructor2() 

     ' Create a generic List of types and add the known types
     ' to the collection.
     Dim knownTypeList As New List(Of Type)
     knownTypeList.Add(GetType(PurchaseOrder))
     knownTypeList.Add(GetType(PurchaseOrderV3))
     
     ' Create a DatatContractSerializer with the collection.
     Dim ser2 As New DataContractSerializer(GetType(Orders), knownTypeList)

     ' Other code not shown.
End Sub

另請參閱

適用於

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs

初始化 DataContractSerializer 類別的新執行個體,以序列化或還原序列化所指定型別的物件。 這個方法也會透過兩個 XmlDictionaryString 參數來指定根 XML 項目和命名空間,以及可能存在於物件圖形內之已知型別的清單。

public:
 DataContractSerializer(Type ^ type, System::Xml::XmlDictionaryString ^ rootName, System::Xml::XmlDictionaryString ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes);
public DataContractSerializer (Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes);
public DataContractSerializer (Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Collections.Generic.IEnumerable<Type>? knownTypes);
new System.Runtime.Serialization.DataContractSerializer : Type * System.Xml.XmlDictionaryString * System.Xml.XmlDictionaryString * seq<Type> -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As XmlDictionaryString, rootNamespace As XmlDictionaryString, knownTypes As IEnumerable(Of Type))

參數

type
Type

已序列化或還原序列化之執行個體的型別。

rootName
XmlDictionaryString

XmlDictionaryString,其中包含內容的根項目名稱。

rootNamespace
XmlDictionaryString

XmlDictionaryString,其中包含根項目的命名空間。

knownTypes
IEnumerable<Type>

IEnumerable<T>Type,其中包含可能存在物件圖形中的已知型別。

範例

下列範例會建立 DataContractSerializer 的執行個體,該執行個體會指定要序列化或還原序列化的型別,以及要在其中讀取或將 XML 文件寫入其中的 XML 名稱和命名空間 (相同於 XmlDictionaryString 物件的處理方式)。 此程式碼也會建立 IEnumerable<T> 的執行個體,以包含在序列化或還原序列化期間所使用的已知型別。

public static void Constructor6()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    // Create an XmlDictionary and add values to it.
    XmlDictionary d = new XmlDictionary();
    XmlDictionaryString name_value = d.Add("Customer");
    XmlDictionaryString ns_value = d.Add("http://www.contoso.com");

    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        name_value,
        ns_value,
        knownTypeList);

    // Other code not shown.
}
Public Shared Sub Constructor6() 
    ' Create a generic List of types and add the known types
    ' to the collection.
    Dim knownTypeList As New List(Of Type)
    knownTypeList.Add(GetType(PurchaseOrder))
    knownTypeList.Add(GetType(PurchaseOrderV3))

    ' Create an XmlDictionary and add values to it.
    Dim d As New XmlDictionary()
    Dim name_value As XmlDictionaryString = d.Add("Customer")
    Dim ns_value As XmlDictionaryString = d.Add("http://www.contoso.com")
    
    Dim ser As New DataContractSerializer(GetType(Person), _
    name_value, _
    ns_value, _
    knownTypeList)

    ' Other code not shown.
 End Sub

另請參閱

適用於