Страница: 1 |
|
Вопрос: NET CF : Сериализация и классы
|
Добавлено: 22.03.10 18:20
|
|
Автор вопроса: notricky
|
Подскажите пожалуйста, каким образом можно или лучше всего работать с внешними файлами в компакт фреймворке?
Почитал вот эту тему: http://www.vbnet.ru/forum/show.aspx?id=175858&page=1
В особенности сообщение BG Алексея.
Понравилось - исправил все структуры (т.к. по аналогии делал как в Дельфях) на классы, но теперь ума не приложу как их сериализовать, т.к. БинариФорматтера нету в .НЕТ КФ.
Хочется именно БинариСериализацию, потому как до этого была реализована ХМЛСериализация, как единственная, которая есть в компакте.
Но сейчас уже и ей не могу сериализовать классы. Теперь мучаюсь - делаю вручную записать/чтение в бинари файле. геморройно.
Кроме этого, пробовал CompactFormatter и CompactFormatterPlus. Так и не понял, как с ними работать. При установке CompactFormatter, почему-то у него отсутствует пространство имен Surrogate, хотя в мануале и в сорсах оно объявлено.
Приведу пример класса для сериализации CSizeList:
<Serializable()> Public Class CSize
Private _Name As String
Private _Size As String
Private _Dt As Double
Sub New(ByVal Name As String, ByVal Size As String, ByVal Dt As Double)
_Name = Name
_Size = Size
_Dt = Dt
End Sub
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property Size() As String
Get
Return _Size
End Get
Set(ByVal value As String)
_Size = value
End Set
End Property
Public Property Dt() As Double
Get
Return _Dt
End Get
Set(ByVal value As Double)
_Dt = value
End Set
End Property
End Class
<Serializable()> Public Class CSizeList
Implements IEnumerable(Of CSize)
Dim L As New Generic.List(Of CSize)
Public ReadOnly Property Count() As Integer
Get
Return L.Count
End Get
End Property
Public Function GetListIndexByControl(ByVal Cont As Control)
If Cont.Tag Is Nothing Then
Return -1
End If
Dim ts As STagSize = CType(Cont.Tag, STagSize)
If ts.Index < Count Then
Return ts.Index
Else
Return -1
End If
End Function
Public Function Add(ByVal Item As CSize)
L.Add(Item)
Return Count - 1
End Function
Public Function Add(ByVal Name As String, ByVal Size As String, ByVal Dt As Double) As Integer 'Возвращает новый Index
L.Add(New CSize(Name, Size, Dt))
Return L.Count - 1
End Function
Public Function AddByControl(ByRef Cont As Control, ByVal Name As String, ByVal Size As String, ByVal Dt As Double) As Integer
Dim i = GetListIndexByControl(Cont)
If i <> -1 Then
L(i).Name = Name
L(i).Size = Size
L(i).Dt = Dt
Else
Cont.Tag = CType(New STagSize With {.Index = Add(Name, Size, Dt), .Dt = Dt}, Object)
End If
End Function
Public Sub Clear()
L.Clear()
End Sub
Public Function GetSize(ByVal Index As Integer) As CSize
If Index < L.Count Then
Return L(Index)
Else
Return Nothing
End If
End Function
Public Sub DeleteItem(ByVal Index As Integer)
L.RemoveAt(Index)
End Sub
Public Sub DeleteSizeControl(ByVal Cont As Control)
Dim i As Integer = CType(Cont.Tag, Integer)
DeleteItem(i)
Cont.Dispose()
End Sub
Public Function OrderByDate(Optional ByVal Dt As Double = -1) As IEnumerable(Of CSize)
If Dt <> -1 Then
Return Me.OrderBy(Function(CSize) CSize.Dt).Where(Function(CSize) CSize.Dt = Dt)
Else
Return Me.OrderBy(Function(CSize) CSize.Dt)
End If
End Function
Public Function Save(ByVal FileName As String) As Boolean
Try
Dim fs As New FileStream(FileName, FileMode.OpenOrCreate)
Dim bw As New BinaryWriter(fs)
For Each i In Me.L
bw.Write(i.Dt)
bw.Write(i.Name)
bw.Write(i.Size)
Next i
fs.Flush()
fs.Close()
bw.Close()
Return True
Catch
Return False
End Try
End Function
Public Shared Function OpenSizeListDB(ByVal FileName As String) As CSizeList
Dim db As New CSizeList
Try
Dim FS As New FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
Dim br As New BinaryReader(FS)
While FS.Length > FS.Position
Dim Item As New CSize("", -1, 0)
Item.Dt = br.ReadDouble
Item.Name = br.ReadString
Item.Size = br.ReadString
db.Add(Item)
End While
FS.Close()
br.Close()
Catch
End Try
Return db
End Function
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of CSize) Implements System.Collections.Generic.IEnumerable(Of CSize).GetEnumerator
Return L.GetEnumerator()
End Function
Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return L.GetEnumerator()
End Function
End Class
Ответить
|
Номер ответа: 1 Автор ответа: notricky
Вопросов: 1 Ответов: 1
|
Профиль | | #1
|
Добавлено: 22.03.10 18:21
|
Для лучшей читабельности: (незнал, что другие теги для сорсов)
- <Serializable()> Public Class CSize
- Private _Name As String
- Private _Size As String
- Private _Dt As Double
- Sub New(ByVal Name As String, ByVal Size As String, ByVal Dt As Double)
- _Name = Name
- _Size = Size
- _Dt = Dt
- End Sub
- Public Property Name() As String
- Get
- Return _Name
- End Get
- Set(ByVal value As String)
- _Name = value
- End Set
- End Property
- Public Property Size() As String
- Get
- Return _Size
- End Get
- Set(ByVal value As String)
- _Size = value
- End Set
- End Property
- Public Property Dt() As Double
- Get
- Return _Dt
- End Get
- Set(ByVal value As Double)
- _Dt = value
- End Set
- End Property
- End Class
- <Serializable()> Public Class CSizeList
- Implements IEnumerable(Of CSize)
- Dim L As New Generic.List(Of CSize)
- Public ReadOnly Property Count() As Integer
- Get
- Return L.Count
- End Get
- End Property
- Public Function GetListIndexByControl(ByVal Cont As Control)
- If Cont.Tag Is Nothing Then
- Return -1
- End If
- Dim ts As STagSize = CType(Cont.Tag, STagSize)
- If ts.Index < Count Then
- Return ts.Index
- Else
- Return -1
- End If
- End Function
- Public Function Add(ByVal Item As CSize)
- L.Add(Item)
- Return Count - 1
- End Function
- Public Function Add(ByVal Name As String, ByVal Size As String, ByVal Dt As Double) As Integer
- L.Add(New CSize(Name, Size, Dt))
- Return L.Count - 1
- End Function
- Public Function AddByControl(ByRef Cont As Control, ByVal Name As String, ByVal Size As String, ByVal Dt As Double) As Integer
- Dim i = GetListIndexByControl(Cont)
- If i <> -1 Then
- L(i).Name = Name
- L(i).Size = Size
- L(i).Dt = Dt
- Else
- Cont.Tag = CType(New STagSize With {.Index = Add(Name, Size, Dt), .Dt = Dt}, Object)
- End If
- End Function
- Public Sub Clear()
- L.Clear()
- End Sub
- Public Function GetSize(ByVal Index As Integer) As CSize
- If Index < L.Count Then
- Return L(Index)
- Else
- Return Nothing
- End If
- End Function
- Public Sub DeleteItem(ByVal Index As Integer)
- L.RemoveAt(Index)
- End Sub
- Public Sub DeleteSizeControl(ByVal Cont As Control)
- Dim i As Integer = CType(Cont.Tag, Integer)
- DeleteItem(i)
- Cont.Dispose()
- End Sub
- Public Function OrderByDate(Optional ByVal Dt As Double = -1) As IEnumerable(Of CSize)
- If Dt <> -1 Then
- Return Me.OrderBy(Function(CSize) CSize.Dt).Where(Function(CSize) CSize.Dt = Dt)
- Else
- Return Me.OrderBy(Function(CSize) CSize.Dt)
- End If
- End Function
- Public Function Save(ByVal FileName As String) As Boolean
- Try
- Dim fs As New FileStream(FileName, FileMode.OpenOrCreate)
- Dim bw As New BinaryWriter(fs)
- For Each i In Me.L
- bw.Write(i.Dt)
- bw.Write(i.Name)
- bw.Write(i.Size)
- Next i
- fs.Flush()
- fs.Close()
- bw.Close()
- Return True
- Catch
- Return False
- End Try
- End Function
- Public Shared Function OpenSizeListDB(ByVal FileName As String) As CSizeList
- Dim db As New CSizeList
- Try
- Dim FS As New FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
- Dim br As New BinaryReader(FS)
- While FS.Length > FS.Position
- Dim Item As New CSize("", -1, 0)
- Item.Dt = br.ReadDouble
- Item.Name = br.ReadString
- Item.Size = br.ReadString
- db.Add(Item)
- End While
- FS.Close()
- br.Close()
- Catch
- End Try
- Return db
- End Function
-
- Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of CSize) Implements System.Collections.Generic.IEnumerable(Of CSize).GetEnumerator
- Return L.GetEnumerator()
- End Function
- Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
- Return L.GetEnumerator()
- End Function
- End Class
Ответить
|
Страница: 1 |
Поиск по форуму