OnDeserializedAttribute クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<AttributeUsageAttribute(AttributeTargets.Method, Inherited:=False)> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class OnDeserializedAttribute Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Method, Inherited=false)] [ComVisibleAttribute(true)] public sealed class OnDeserializedAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Method, Inherited=false)] [ComVisibleAttribute(true)] public ref class OnDeserializedAttribute sealed : public Attribute

OnDeserializedAttribute は、オブジェクトを逆シリアル化した後、グラフが返される前に、逆シリアル化したオブジェクトの値を修正する必要があるときに使用します。この属性は、IDeserializationCallback インターフェイスの代わりに使用できます。
OnDeserializedAttribute を使用するには、メソッドに StreamingContext パラメータが含まれている必要があります。この属性により、メソッドがシリアル化インフラストラクチャによって呼び出されるようマークされます。また StreamingContext には、実行されるシリアル化のタイプに関する追加のデータが含まれています。次のコードは、その使用法を示しています。
\\ C# [OnDeserializedAttribute()] private void RunThisMethod(StreamingContext context) { // Code not shown. } ' Visual Basic <OnDeserializedAttribute()> _ Private Sub RunThisMethod(context As StreamingContext) ' Code not shown. End Sub
![]() |
---|
コードでは、OnDeserializedAttribute の代わりに OnDeserialized という短い語を使用できます。 |

次の例では、OnDeserializedAttribute、OnSerializingAttribute、OnSerializedAttribute、および OnDeserializingAttribute の各属性を、クラス内のメソッドに適用しています。
Imports System 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 "data.xml" 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() 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.'
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 "data.xml" 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(); } 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.'

System.Attribute
System.Runtime.Serialization.OnDeserializedAttribute


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


OnDeserializedAttribute コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


OnDeserializedAttribute プロパティ
OnDeserializedAttribute メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 ( Attribute から継承されます。) |
![]() | GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 ( Attribute から継承されます。) |
![]() | GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 ( Attribute から継承されます。) |
![]() | GetHashCode | このインスタンスのハッシュ コードを返します。 ( Attribute から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 ( Attribute から継承されます。) |
![]() | IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 ( Attribute から継承されます。) |
![]() | Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 ( Attribute から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

OnDeserializedAttribute メンバ
メソッドに適用すると、そのメソッドがオブジェクトの逆シリアル化の直後に呼び出されるよう指定します。
OnDeserializedAttribute データ型で公開されるメンバを以下の表に示します。



名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 ( Attribute から継承されます。) |
![]() | GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 (Attribute から継承されます。) |
![]() | GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 (Attribute から継承されます。) |
![]() | GetHashCode | このインスタンスのハッシュ コードを返します。 (Attribute から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。) |
![]() | IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 (Attribute から継承されます。) |
![]() | Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

- OnDeserializedAttributeのページへのリンク