반응형
* 객체 직렬화 (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
반응형
'VB.NET Programming' 카테고리의 다른 글
[VBNET] INIFile Create & Read & Write Example (0) | 2019.09.26 |
---|---|
[VBNET] 레지스트리(Registry) Create & Delete & Read & Write (0) | 2019.09.23 |
[VBNET] BackGround Worker 를 이용 백그라운드 스레드 (0) | 2019.09.17 |
[VBNET] 크로스 스레드 (Cross Thread) 예제 (0) | 2019.09.16 |
[VBNET] 델리 게이트 (Delegate) - 델리게이트란? , 선언 방법과 간단한 예제 (0) | 2019.09.13 |