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

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

OnSerializedAttribute クラス

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

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

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

<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Method, Inherited:=False)> _
Public NotInheritable Class
 OnSerializedAttribute
    Inherits Attribute
Dim instance As OnSerializedAttribute
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Method, Inherited=false)]
 
public sealed class OnSerializedAttribute :
 Attribute
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets::Method, Inherited=false)]
 
public ref class OnSerializedAttribute sealed
 : public Attribute
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Method, Inherited=false)
 */ 
public final class OnSerializedAttribute extends
 Attribute
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Method, Inherited=false)
 
public final class OnSerializedAttribute extends
 Attribute
解説解説

OnSerializedAttribute使用すると、シリアル化実行後にオブジェクト操作できます

OnSerializedAttribute使用するには、メソッドに StreamingContext パラメータ含まれている必要があります。この属性により、メソッドシリアル化インフラストラクチャによって呼び出されるようマークされます。また StreamingContext には、実行されるシリアル化タイプに関する追加データ含まれています。次のコードは、その使用法示してます。

\\ C#
[OnSerializedAttribute()]
internal void RunThisMethod(StreamingContext context)
{ 
    // Code not shown.
}
' Visual Basic
<OnSerializedAttribute()> _
Private Sub RunThisMethod(context As StreamingContext)
    ' Code not shown.
End Sub
メモメモ

コードでは、OnSerializedAttribute代わりに OnSerialized という短い語使用できます

					
使用例使用例

次の例では、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.OnSerializedAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
OnSerializedAttribute メンバ
System.Runtime.Serialization 名前空間
BinaryFormatter
OnDeserializedAttribute クラス
OnDeserializingAttribute クラス
OnSerializingAttribute
SoapFormatter


このページでは「.NET Framework クラス ライブラリ リファレンス」からOnSerializedAttribute クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からOnSerializedAttribute クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からOnSerializedAttribute クラス を検索

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

辞書ショートカット

すべての辞書の索引

「OnSerializedAttribute クラス」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS