OnDeserializingAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
套用至方法時,指定在還原序列化物件圖形中的物件期間呼叫該方法。 還原序列化相對於圖形中其他物件的順序不具決定性。
public ref class OnDeserializingAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
public sealed class OnDeserializingAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class OnDeserializingAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
type OnDeserializingAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type OnDeserializingAttribute = class
inherit Attribute
Public NotInheritable Class OnDeserializingAttribute
Inherits Attribute
- 繼承
- 屬性
範例
下列範例會將 OnDeserializedAttribute 、 OnSerializingAttribute 、 OnSerializedAttribute 和 OnDeserializingAttribute 屬性套用至類別中的方法。
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.'
備註
使用 在 OnDeserializingAttribute 還原序列化期間設定預設值。 例如,如果您要還原序列化的型別不包含建構函式,請建立方法來設定 實例中任何欄位的值,並將 屬性套用至 方法。
若要使用 OnDeserializingAttribute ,方法必須包含 StreamingContext 參數。 屬性會標示序列化基礎結構所呼叫的方法,並提供 StreamingContext 有關進行序列化類型的其他資料。 使用方式會顯示在下列程式碼中:
[OnDeserializing]
private void SetValuesOnDeserializing(StreamingContext context)
{
// Code not shown.
}
<OnDeserializing()> _
Private Sub SetValuesOnDeserializing(ByVal context As StreamingContext)
' Code not shown.
End Sub
注意
在程式碼中,您可以使用字組 OnDeserializing
來代替較長的 OnDeserializingAttribute。
建構函式
OnDeserializingAttribute() |
初始化 OnDeserializingAttribute 類別的新執行個體。 |
屬性
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) |
將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承來源 Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
擷取物件的類型資訊,可以用來取得介面的類型資訊。 (繼承來源 Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
擷取物件提供的類型資訊介面數目 (0 或 1)。 (繼承來源 Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供物件所公開的屬性和方法的存取權。 (繼承來源 Attribute) |