반응형

* 객체 직렬화 (Serialization)

 - 객체를 메모리나 파일, 데이터 베이스 등 에 저장할 때 쓰이는 방식

 

* 예제

 

메인 화면

Form1.vb

 

Imports System.Runtime.Serialization
Imports System.Collections

Public Class Form1
    Dim TT As T_TEST
    Dim t As clsTEST = New clsTEST

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TT.strTEST = "객체 직렬화"
        t.FnTest = 15430305

        Using stm As System.IO.Stream = System.IO.File.Open("ect.class", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite)
            Dim bf As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
            '객체 직렬화 저장...
            bf.Serialize(stm, t)
            stm.Close()
        End Using
        TextBox1.Text += "Object Class" + vbCrLf
        TextBox1.Text += "lTest Value : " + t.FnTest.ToString() + vbCrLf
        TextBox1.Text += "객체 클래스 저장 완료..." + vbCrLf + vbCrLf


        Using stm As System.IO.Stream = System.IO.File.Open("ect.struct", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite)
            Dim bf As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
            '객체 직렬화 저장...
            bf.Serialize(stm, TT)
            stm.Close()
        End Using

        TextBox1.Text += "Object Struct" + vbCrLf
        TextBox1.Text += "sText Value : " + TT.strTEST + vbCrLf
        TextBox1.Text += "객체 구조체 저장 완료..." + vbCrLf

        TextBox1.Text += "=========================" + vbCrLf

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Using stm As System.IO.Stream = System.IO.File.Open("ect.class", System.IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim bf As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

            '역직렬화 후 객체 클래스로 형변환...
            Dim a As clsTEST = CType(bf.Deserialize(stm), clsTEST)
            stm.Close()

            TextBox1.Text += "Object Class" + vbCrLf
            TextBox1.Text += "lTest Value : " + a.FnTest().ToString() + vbCrLf
            TextBox1.Text += "객체 클래스 불러오기 완료..." + vbCrLf + vbCrLf

        End Using


        Using stm As System.IO.Stream = System.IO.File.Open("ect.struct", System.IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim bf As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

            '역직렬화 후 객체 구조체로 형 변환...
            Dim a As T_TEST = CType(bf.Deserialize(stm), T_TEST)
            stm.Close()

            TextBox1.Text += "Object Struct" + vbCrLf
            TextBox1.Text += "sText Value : " + a.strTEST + vbCrLf
            TextBox1.Text += "객체 구조체 불러오기 완료..." + vbCrLf

        End Using
    End Sub
End Class


<Serializable()> Public Structure T_TEST
    Public strTEST As String
End Structure


<Serializable()> Public Class clsTEST
    Dim lTest As Long

    Public Sub New()
        lTest = 5
    End Sub

    Property FnTest() As Integer
        Get
            Return lTest
        End Get
        Set(ByVal value As Integer)
            lTest = value
        End Set
    End Property

    Public Function Test() As Long
        Return lTest
    End Function


End Class

* 객체 직렬화를 위해서는 그 선언 객체에 반드시 <Serializable()> 을 붙여 줘야 됨.

 

 

[C#] ▼

https://kdsoft-zeros.tistory.com/26

 

[C#] 객체 파일 저장 및 불러오기 예제 (Serialization)

* 객체 직렬화 (Serialization) - 객체를 메모리나 파일 , 데이터베이스에 저장할 때 쓰이는 방식 * 예제 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System..

kdsoft-zeros.tistory.com

 

반응형

+ Recent posts