OnDeserializedAttribute 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当应用于方法时,指定在反序列化对象关系图中的对象之后立即调用该方法。 相对于关系图中的其他对象,该反序列化的顺序是不确定的。
public ref class OnDeserializedAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
public sealed class OnDeserializedAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class OnDeserializedAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
type OnDeserializedAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type OnDeserializedAttribute = class
inherit Attribute
Public NotInheritable Class OnDeserializedAttribute
Inherits Attribute
- 继承
- 属性
示例
The following example applies the OnDeserializedAttribute, OnSerializingAttribute, OnSerializedAttribute, and OnDeserializingAttribute attributes to methods in a class.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class Test
{
public static void Main()
{
// Create a new TestSimpleObject object.
TestSimpleObject obj = new TestSimpleObject();
Console.WriteLine("\n Before serialization the object contains: ");
obj.Print();
// Open a file and serialize the object into binary format.
Stream stream = File.Open("DataFile.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(stream, obj);
// Print the object again to see the effect of the
//OnSerializedAttribute.
Console.WriteLine("\n After serialization the object contains: ");
obj.Print();
// Set the original variable to null.
obj = null;
stream.Close();
// Open the file "DataFile.dat" and deserialize the object from it.
stream = File.Open("DataFile.dat", FileMode.Open);
// Deserialize the object from the data file.
obj = (TestSimpleObject)formatter.Deserialize(stream);
Console.WriteLine("\n After deserialization the object contains: ");
obj.Print();
Console.ReadLine();
}
catch (SerializationException se)
{
Console.WriteLine("Failed to serialize. Reason: " + se.Message);
throw;
}
catch (Exception exc)
{
Console.WriteLine("An exception occurred. Reason: " + exc.Message);
throw;
}
finally
{
stream.Close();
obj = null;
formatter = null;
}
}
}
// This is the object that will be serialized and deserialized.
[Serializable()]
public class TestSimpleObject
{
// This member is serialized and deserialized with no change.
public int member1;
// The value of this field is set and reset during and
// after serialization.
private string member2;
// This field is not serialized. The OnDeserializedAttribute
// is used to set the member value after serialization.
[NonSerialized()]
public string member3;
// This field is set to null, but populated after deserialization.
private string member4;
// Constructor for the class.
public TestSimpleObject()
{
member1 = 11;
member2 = "Hello World!";
member3 = "This is a nonserialized value";
member4 = null;
}
public void Print()
{
Console.WriteLine("member1 = '{0}'", member1);
Console.WriteLine("member2 = '{0}'", member2);
Console.WriteLine("member3 = '{0}'", member3);
Console.WriteLine("member4 = '{0}'", member4);
}
[OnSerializing()]
internal void OnSerializingMethod(StreamingContext context)
{
member2 = "This value went into the data file during serialization.";
}
[OnSerialized()]
internal void OnSerializedMethod(StreamingContext context)
{
member2 = "This value was reset after serialization.";
}
[OnDeserializing()]
internal void OnDeserializingMethod(StreamingContext context)
{
member3 = "This value was set during deserialization";
}
[OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context)
{
member4 = "This value was set after deserialization.";
}
}
// Output:
// Before serialization the object contains:
// member1 = '11'
// member2 = 'Hello World!'
// member3 = 'This is a nonserialized value'
// member4 = ''
//
// After serialization the object contains:
// member1 = '11'
// member2 = 'This value was reset after serialization.'
// member3 = 'This is a nonserialized value'
// member4 = ''
//
// After deserialization the object contains:
// member1 = '11'
// member2 = 'This value went into the data file during serialization.'
// member3 = 'This value was set during deserialization'
// member4 = 'This value was set after deserialization.'
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Public Class Test
Public Shared Sub Main()
' Create a new TestSimpleObject object.
Dim obj As New TestSimpleObject()
Console.WriteLine(vbLf + " Before serialization the object contains: ")
obj.Print()
' Open a file and serialize the object into binary format.
Dim stream As Stream = File.Open("DataFile.dat", FileMode.Create)
Dim formatter As New BinaryFormatter()
Try
formatter.Serialize(stream, obj)
' Print the object again to see the effect of the
'OnSerializedAttribute.
Console.WriteLine(vbLf + " After serialization the object contains: ")
obj.Print()
' Set the original variable to null.
obj = Nothing
stream.Close()
' Open the file "DataFile.dat" and deserialize the object from it.
stream = File.Open("DataFile.dat", FileMode.Open)
' Deserialize the object from the data file.
obj = CType(formatter.Deserialize(stream), TestSimpleObject)
Console.WriteLine(vbLf + " After deserialization the object contains: ")
obj.Print()
Console.ReadLine()
Catch se As SerializationException
Console.WriteLine("Failed to serialize. Reason: " + se.Message)
Throw
Catch exc As Exception
Console.WriteLine("An exception occurred. Reason: " + exc.Message)
Throw
Finally
stream.Close()
obj = Nothing
formatter = Nothing
End Try
End Sub
End Class
' This is the object that will be serialized and deserialized.
<Serializable()> _
Public Class TestSimpleObject
' This member is serialized and deserialized with no change.
Public member1 As Integer
' The value of this field is set and reset during and
' after serialization.
Private member2 As String
' This field is not serialized. The OnDeserializedAttribute
' is used to set the member value after serialization.
<NonSerialized()> _
Public member3 As String
' This field is set to null, but populated after deserialization.
Private member4 As String
' Constructor for the class.
Public Sub New()
member1 = 11
member2 = "Hello World!"
member3 = "This is a nonserialized value"
member4 = Nothing
End Sub
Public Sub Print()
Console.WriteLine("member1 = '{0}'", member1)
Console.WriteLine("member2 = '{0}'", member2)
Console.WriteLine("member3 = '{0}'", member3)
Console.WriteLine("member4 = '{0}'", member4)
End Sub
<OnSerializing()> _
Friend Sub OnSerializingMethod(ByVal context As StreamingContext)
member2 = "This value went into the data file during serialization."
End Sub
<OnSerialized()> _
Friend Sub OnSerializedMethod(ByVal context As StreamingContext)
member2 = "This value was reset after serialization."
End Sub
<OnDeserializing()> _
Friend Sub OnDeserializingMethod(ByVal context As StreamingContext)
member3 = "This value was set during deserialization"
End Sub
<OnDeserialized()> _
Friend Sub OnDeserializedMethod(ByVal context As StreamingContext)
member4 = "This value was set after deserialization."
End Sub
End Class
' Output:
' Before serialization the object contains:
' member1 = '11'
' member2 = 'Hello World!'
' member3 = 'This is a nonserialized value'
' member4 = ''
'
' After serialization the object contains:
' member1 = '11'
' member2 = 'This value was reset after serialization.'
' member3 = 'This is a nonserialized value'
' member4 = ''
'
' After deserialization the object contains:
' member1 = '11'
' member2 = 'This value went into the data file during serialization.'
' member3 = 'This value was set during deserialization'
' member4 = 'This value was set after deserialization.'
注解
OnDeserializedAttribute在反序列化对象和返回图形之前,需要修复反序列化对象上的值。 可以使用此属性而不是 IDeserializationCallback 接口。
若要使用, OnDeserializedAttribute该方法必须包含参数 StreamingContext 。 该属性将标记序列化基础结构调用的方法, StreamingContext 并提供有关所发生的序列化类型的附加数据。 以下代码中显示了使用情况:
[OnDeserialized]
private void SetValuesOnDeserialized(StreamingContext context)
{
// Code not shown.
}
<OnDeserialized()> _
Private Sub SetValuesOnDeserialized(ByVal context As StreamingContext)
' Code not shown.
End Sub
备注
在代码中,可以使用 OnDeserialized
一词来代替较长的 OnDeserializedAttribute。
构造函数
OnDeserializedAttribute() |
初始化 OnDeserializedAttribute 类的新实例。 |
属性
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) |