DataContractSerializer 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 DataContractSerializer 类的新实例。
重载
DataContractSerializer(Type)
- Source:
- DataContractSerializer.cs
- Source:
- DataContractSerializer.cs
- Source:
- 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 构造来保留图中的对象引用数据、自定义序列化的代理项、指定包含内容的 XML 元素和命名空间的 XmlDictionaryString 参数以及在运行时映射 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
用于将 xsi:type
声明映射到数据协定类型的 DataContractResolver 的实现。
适用于
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
用于将 xsi:type
声明映射到数据协定类型的 DataContractResolver 的实现。
适用于
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
指定根 XML 命名空间的 XmlDictionaryString。
- 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> 实例以包含在反序列化期间使用的类型。 该代码还将 和 preserveObjectReferences
参数设置为 ignoreExtensionDataObject
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
另请参阅
- MaxItemsInObjectGraph
- PreserveObjectReferences
- IgnoreExtensionDataObject
- IExtensibleDataObject
- ExtensionDataObject
- IDataContractSurrogate
适用于
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> 实例以包含在反序列化期间使用的类型。 该代码还将 和 preserveObjectReferences
参数设置为 ignoreExtensionDataObject
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
另请参阅
- MaxItemsInObjectGraph
- PreserveObjectReferences
- IgnoreExtensionDataObject
- IExtensibleDataObject
- ExtensionDataObject
- KnownTypeAttribute
- IDataContractSurrogate
适用于
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> 实例以包含在序列化或反序列化期间使用的已知类型。 该代码还将 和 preserveObjectReferences
参数设置为 ignoreExtensionDataObject
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
另请参阅
- MaxItemsInObjectGraph
- PreserveObjectReferences
- IgnoreExtensionDataObject
- IExtensibleDataObject
- ExtensionDataObject
- KnownTypeAttribute
- IDataContractSurrogate
适用于
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
用于将 xsi:type
声明映射到数据协定类型的 DataContractResolver 的实现。
注解
警告
此方法在 Windows 应用商店应用中不可用。
适用于
DataContractSerializer(Type, String, String, IEnumerable<Type>)
- Source:
- DataContractSerializer.cs
- Source:
- DataContractSerializer.cs
- Source:
- 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)
- Source:
- DataContractSerializer.cs
- Source:
- DataContractSerializer.cs
- Source:
- DataContractSerializer.cs
使用通过类型为 DataContractSerializer 的参数指定的 XML 根元素和命名空间初始化 XmlDictionaryString 类的新实例,以便序列化或反序列化指定类型的对象。
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)
- Source:
- DataContractSerializer.cs
- Source:
- DataContractSerializer.cs
- Source:
- DataContractSerializer.cs
使用提供的 XML 根元素和命名空间初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。
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)
- Source:
- DataContractSerializer.cs
- Source:
- DataContractSerializer.cs
- Source:
- 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>)
- Source:
- DataContractSerializer.cs
- Source:
- DataContractSerializer.cs
- Source:
- 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>)
- Source:
- DataContractSerializer.cs
- Source:
- DataContractSerializer.cs
- Source:
- 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