Top Index
Yahooは検索方式が変わったせいか、うまくサイト内検索できないなぁ。つーわけでグーグルに変更。
![]() ![]() ![]() |
| 01 | Imports System.IO | |
| 02 | Imports System.Runtime.Serialization.Formatters.Binary | |
| 03 | ||
| 04 | Public Class Form1 | |
| 05 | ||
| 06 | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click | |
| 07 | Dim f1 As New Family With {.Address = "東京都"} | |
| 08 | f1.PersonList.Add(New Person With {.Family = f1, .Id = 1, .Name = "Taro"}) | |
| 09 | f1.PersonList.Add(New Person With {.Family = f1, .Id = 2, .Name = "Hanako"}) | |
| 10 | ||
| 11 | 'シリアライズ | |
| 12 | Using fs As New FileStream("c:\data.bin", FileMode.Create) | |
| 13 | Dim bf As New BinaryFormatter | |
| 14 | bf.Serialize(fs, f1) | |
| 15 | End Using | |
| 16 | ||
| 17 | 'デシリアライズ(擬似ディープコピー) | |
| 18 | Dim f2 As New Family | |
| 19 | Using fs As New FileStream("c:\data.bin", FileMode.Open) | |
| 20 | Dim f As New BinaryFormatter | |
| 21 | f2 = f.Deserialize(fs) | |
| 22 | End Using | |
| 23 | ||
| 24 | Debug.Print(f1.Equals(f1.PersonList(0).Family)) 'True になります | |
| 25 | Debug.Print(f2.Equals(f2.PersonList(0).Family)) 'True になります | |
| 26 | Debug.Print(f1.Equals(f2)) 'False になります | |
| 27 | Debug.Print(f1.PersonList.Equals(f2.PersonList)) 'False になります | |
| 28 | End Sub | |
| 29 | ||
| 30 | End Class | |
| 31 | ||
| 32 | <Serializable()> _ | |
| 33 | Public Class Person | |
| 34 | ||
| 35 | Private _Id As Integer | |
| 36 | Public Property Id() As Integer | |
| 37 | Get | |
| 38 | Return _Id | |
| 39 | End Get | |
| 40 | Set(ByVal value As Integer) | |
| 41 | _Id = value | |
| 42 | End Set | |
| 43 | End Property | |
| 44 | ||
| 45 | Private _Name As String | |
| 46 | Public Property Name() As String | |
| 47 | Get | |
| 48 | Return _Name | |
| 49 | End Get | |
| 50 | Set(ByVal value As String) | |
| 51 | _Name = value | |
| 52 | End Set | |
| 53 | End Property | |
| 54 | ||
| 55 | Private _Famliy As Family | |
| 56 | Public Property Family() As Family | |
| 57 | Get | |
| 58 | Return _Famliy | |
| 59 | End Get | |
| 60 | Set(ByVal value As Family) | |
| 61 | _Famliy = value | |
| 62 | End Set | |
| 63 | End Property | |
| 64 | ||
| 65 | End Class | |
| 66 | ||
| 67 | <Serializable()> _ | |
| 68 | Public Class Family | |
| 69 | ||
| 70 | Private _Address As String | |
| 71 | Public Property Address() As String | |
| 72 | Get | |
| 73 | Return _Address | |
| 74 | End Get | |
| 75 | Set(ByVal value As String) | |
| 76 | _Address = value | |
| 77 | End Set | |
| 78 | End Property | |
| 79 | ||
| 80 | Private _PersonList As New List(Of Person) | |
| 81 | Public Property PersonList() As List(Of Person) | |
| 82 | Get | |
| 83 | Return _PersonList | |
| 84 | End Get | |
| 85 | Set(ByVal value As List(Of Person)) | |
| 86 | _PersonList = value | |
| 87 | End Set | |
| 88 | End Property | |
| 89 | ||
| 90 | End Class |