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

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

OnSerializingAttribute クラス

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

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

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

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

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

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

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

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

使用例使用例

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



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

辞書ショートカット

すべての辞書の索引

「OnSerializingAttribute クラス」の関連用語

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

   

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



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

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

©2024 GRAS Group, Inc.RSS