OnDeserializedAttribute クラスとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > OnDeserializedAttribute クラスの意味・解説 

OnDeserializedAttribute クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

メソッド適用すると、そのメソッドオブジェクトの逆シリアル化直後呼び出されるよう指定します

名前空間: System.Runtime.Serialization
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<AttributeUsageAttribute(AttributeTargets.Method, Inherited:=False)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class
 OnDeserializedAttribute
    Inherits Attribute
Dim instance As OnDeserializedAttribute
[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
/** @attribute AttributeUsageAttribute(AttributeTargets.Method, Inherited=false)
 */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class OnDeserializedAttribute
 extends Attribute
AttributeUsageAttribute(AttributeTargets.Method, Inherited=false)
 
ComVisibleAttribute(true) 
public final class OnDeserializedAttribute
 extends 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.Object
   System.Attribute
    System.Runtime.Serialization.OnDeserializedAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
OnDeserializedAttribute メンバ
System.Runtime.Serialization 名前空間
BinaryFormatter
OnDeserializingAttribute
OnSerializedAttribute
OnSerializingAttribute
SoapFormatter



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「OnDeserializedAttribute クラス」の関連用語

OnDeserializedAttribute クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



OnDeserializedAttribute クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS