DataContractAttribute 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定该类型要定义或实现一个数据协定,并可由序列化程序(如 DataContractSerializer)进行序列化。 若要使其类型可序列化,类型作者必须为其类型定义数据协定。
public ref class DataContractAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Struct, AllowMultiple=false, Inherited=false)]
public sealed class DataContractAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Struct, AllowMultiple=false, Inherited=false)>]
type DataContractAttribute = class
inherit Attribute
Public NotInheritable Class DataContractAttribute
Inherits Attribute
- 继承
- 属性
示例
下面的示例序列化和反序列化了一个名为 Person
的类,该类已应用 DataContractAttribute。 请注意,Namespace 和 Name 属性已设置为对默认设置进行重写的值。
namespace DataContractAttributeExample
{
// Set the Name and Namespace properties to new values.
[DataContract(Name = "Customer", Namespace = "http://www.contoso.com")]
class Person : IExtensibleDataObject
{
// To implement the IExtensibleDataObject interface, you must also
// implement the ExtensionData property.
private ExtensionDataObject extensionDataObjectValue;
public ExtensionDataObject ExtensionData
{
get
{
return extensionDataObjectValue;
}
set
{
extensionDataObjectValue = value;
}
}
[DataMember(Name = "CustName")]
internal string Name;
[DataMember(Name = "CustID")]
internal int ID;
public Person(string newName, int newID)
{
Name = newName;
ID = newID;
}
}
class Test
{
public static void Main()
{
try
{
WriteObject("DataContractExample.xml");
ReadObject("DataContractExample.xml");
Console.WriteLine("Press Enter to end");
Console.ReadLine();
}
catch (SerializationException se)
{
Console.WriteLine
("The serialization operation failed. Reason: {0}",
se.Message);
Console.WriteLine(se.Data);
Console.ReadLine();
}
}
public static void WriteObject(string path)
{
// Create a new instance of the Person class and
// serialize it to an XML file.
Person p1 = new Person("Mary", 1);
// Create a new instance of a StreamWriter
// to read and write the data.
FileStream fs = new FileStream(path,
FileMode.Create);
XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
DataContractSerializer ser =
new DataContractSerializer(typeof(Person));
ser.WriteObject(writer, p1);
Console.WriteLine("Finished writing object.");
writer.Close();
fs.Close();
}
public static void ReadObject(string path)
{
// Deserialize an instance of the Person class
// from an XML file. First create an instance of the
// XmlDictionaryReader.
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
XmlDictionaryReader reader =
XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
// Create the DataContractSerializer instance.
DataContractSerializer ser =
new DataContractSerializer(typeof(Person));
// Deserialize the data and read it from the instance.
Person newPerson = (Person)ser.ReadObject(reader);
Console.WriteLine("Reading this object:");
Console.WriteLine(String.Format("{0}, ID: {1}",
newPerson.Name, newPerson.ID));
fs.Close();
}
}
}
Namespace DataContractAttributeExample
' Set the Name and Namespace properties to new values.
<DataContract(Name := "Customer", [Namespace] := "http://www.contoso.com")> _
Class Person
Implements IExtensibleDataObject
' To implement the IExtensibleDataObject interface, you must also
' implement the ExtensionData property.
Private extensionDataObjectValue As ExtensionDataObject
Public Property ExtensionData() As ExtensionDataObject _
Implements IExtensibleDataObject.ExtensionData
Get
Return extensionDataObjectValue
End Get
Set
extensionDataObjectValue = value
End Set
End Property
<DataMember(Name := "CustName")> _
Friend Name As String
<DataMember(Name := "CustID")> _
Friend ID As Integer
Public Sub New(ByVal newName As String, ByVal newID As Integer)
Name = newName
ID = newID
End Sub
End Class
Class Test
Public Shared Sub Main()
Try
WriteObject("DataContractExample.xml")
ReadObject("DataContractExample.xml")
Console.WriteLine("Press Enter to end")
Console.ReadLine()
Catch se As SerializationException
Console.WriteLine("The serialization operation failed. Reason: {0}", _
se.Message)
Console.WriteLine(se.Data)
Console.ReadLine()
End Try
End Sub
Public Shared Sub WriteObject(ByVal path As String)
' Create a new instance of the Person class and
' serialize it to an XML file.
Dim p1 As New Person("Mary", 1)
' Create a new instance of a StreamWriter
' to read and write the data.
Dim fs As New FileStream(path, FileMode.Create)
Dim writer As XmlDictionaryWriter = XmlDictionaryWriter.CreateTextWriter(fs)
Dim ser As New DataContractSerializer(GetType(Person))
ser.WriteObject(writer, p1)
Console.WriteLine("Finished writing object.")
writer.Close()
fs.Close()
End Sub
Public Shared Sub ReadObject(ByVal path As String)
' Deserialize an instance of the Person class
' from an XML file. First create an instance of the
' XmlDictionaryReader.
Dim fs As New FileStream(path, FileMode.OpenOrCreate)
Dim reader As XmlDictionaryReader = XmlDictionaryReader. _
CreateTextReader(fs, New XmlDictionaryReaderQuotas())
' Create the DataContractSerializer instance.
Dim ser As New DataContractSerializer(GetType(Person))
' Deserialize the data and read it from the instance.
Dim newPerson As Person = CType(ser.ReadObject(reader), Person)
Console.WriteLine("Reading this object:")
Console.WriteLine(String.Format("{0}, ID: {1}", newPerson.Name, newPerson.ID))
fs.Close()
End Sub
End Class
End Namespace
注解
有关此 API 的详细信息,请参阅 DataContractAttribute 的补充 API 备注。
构造函数
DataContractAttribute() |
初始化 DataContractAttribute 类的新实例。 |
属性
IsNameSetExplicitly |
获取 Name 是否已显式设置。 |
IsNamespaceSetExplicitly |
获取 Namespace 是否已显式设置。 |
IsReference |
获取或设置一个值,该值指示是否保留对象引用数据。 |
IsReferenceSetExplicitly |
获取 IsReference 是否已显式设置。 |
Name |
获取或设置类型的数据协定的名称。 |
Namespace |
获取或设置类型的数据协定的命名空间。 |
TypeId |
在派生类中实现时,获取此 Attribute 的唯一标识符。 (继承自 Attribute) |
方法
Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute) |
GetHashCode() |
返回此实例的哈希代码。 (继承自 Attribute) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
IsDefaultAttribute() |
在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute) |
Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
显式接口实现
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。 (继承自 Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。 (继承自 Attribute) |