Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - .NET

Страница: 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  

Номер ответа: 1
Автор ответа:
 notricky



Вопросов: 1
Ответов: 1
 Профиль | | #1 Добавлено: 22.03.10 18:21
Для лучшей читабельности: (незнал, что другие теги для сорсов)
  1.     <Serializable()> Public Class  CSize
  2.         Private _Name As String
  3.         Private _Size As String
  4.         Private _Dt As Double
  5.         Sub New(ByVal Name As String, ByVal Size As String, ByVal Dt As Double)
  6.             _Name = Name
  7.             _Size = Size
  8.             _Dt = Dt
  9.         End Sub
  10.         Public Property Name() As String
  11.             Get
  12.                 Return _Name
  13.             End Get
  14.             Set(ByVal value As String)
  15.                 _Name = value
  16.             End Set
  17.         End Property
  18.         Public Property Size() As String
  19.             Get
  20.                 Return _Size
  21.             End Get
  22.             Set(ByVal value As String)
  23.                 _Size = value
  24.             End Set
  25.         End Property
  26.         Public Property Dt() As Double
  27.             Get
  28.                 Return _Dt
  29.             End Get
  30.             Set(ByVal value As Double)
  31.                 _Dt = value
  32.             End Set
  33.         End Property
  34.     End Class
  35.     <Serializable()> Public Class CSizeList
  36.         Implements IEnumerable(Of CSize)
  37.         Dim L As New Generic.List(Of CSize)
  38.         Public ReadOnly Property Count() As Integer
  39.             Get
  40.                 Return L.Count
  41.             End Get
  42.         End Property
  43.         Public Function GetListIndexByControl(ByVal Cont As Control)
  44.             If Cont.Tag Is Nothing Then
  45.                 Return -1
  46.             End If
  47.             Dim ts As STagSize = CType(Cont.Tag, STagSize)
  48.             If ts.Index < Count Then
  49.                 Return ts.Index
  50.             Else
  51.                 Return -1
  52.             End If
  53.         End Function
  54.         Public Function Add(ByVal Item As CSize)
  55.             L.Add(Item)
  56.             Return Count - 1
  57.         End Function
  58.         Public Function Add(ByVal Name As String, ByVal Size As String, ByVal Dt As Double) As Integer 'Возвращает новый Index
  59.             L.Add(New CSize(Name, Size, Dt))
  60.             Return L.Count - 1
  61.         End Function
  62.         Public Function AddByControl(ByRef Cont As Control, ByVal Name As String, ByVal Size As String, ByVal Dt As Double) As Integer
  63.             Dim i = GetListIndexByControl(Cont)
  64.             If i <> -1 Then
  65.                 L(i).Name = Name
  66.                 L(i).Size = Size
  67.                 L(i).Dt = Dt
  68.             Else
  69.                 Cont.Tag = CType(New STagSize With {.Index = Add(Name, Size, Dt), .Dt = Dt}, Object)
  70.             End If
  71.         End Function
  72.         Public Sub Clear()
  73.             L.Clear()
  74.         End Sub
  75.         Public Function GetSize(ByVal Index As Integer) As CSize
  76.             If Index < L.Count Then
  77.                 Return L(Index)
  78.             Else
  79.                 Return Nothing
  80.             End If
  81.         End Function
  82.         Public Sub DeleteItem(ByVal Index As Integer)
  83.             L.RemoveAt(Index)
  84.         End Sub
  85.         Public Sub DeleteSizeControl(ByVal Cont As Control)
  86.             Dim i As Integer = CType(Cont.Tag, Integer)
  87.             DeleteItem(i)
  88.             Cont.Dispose()
  89.         End Sub
  90.         Public Function OrderByDate(Optional ByVal Dt As Double = -1) As IEnumerable(Of CSize)
  91.             If Dt <> -1 Then
  92.                 Return Me.OrderBy(Function(CSize) CSize.Dt).Where(Function(CSize) CSize.Dt = Dt)
  93.             Else
  94.                 Return Me.OrderBy(Function(CSize) CSize.Dt)
  95.             End If
  96.         End Function
  97.         Public Function Save(ByVal FileName As String) As Boolean
  98.             Try
  99.                 Dim fs As New FileStream(FileName, FileMode.OpenOrCreate)
  100.                 Dim bw As New BinaryWriter(fs)
  101.                 For Each i In Me.L
  102.                     bw.Write(i.Dt)
  103.                     bw.Write(i.Name)
  104.                     bw.Write(i.Size)
  105.                 Next i
  106.                 fs.Flush()
  107.                 fs.Close()
  108.                 bw.Close()
  109.                 Return True
  110.             Catch
  111.                 Return False
  112.             End Try
  113.         End Function
  114.         Public Shared Function OpenSizeListDB(ByVal FileName As String) As CSizeList
  115.             Dim db As New CSizeList
  116.             Try
  117.                 Dim FS As New FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
  118.                 Dim br As New BinaryReader(FS)
  119.                 While FS.Length > FS.Position
  120.                     Dim Item As New CSize("", -1, 0)
  121.                     Item.Dt = br.ReadDouble
  122.                     Item.Name = br.ReadString
  123.                     Item.Size = br.ReadString
  124.                     db.Add(Item)
  125.                 End While
  126.                 FS.Close()
  127.                 br.Close()
  128.             Catch
  129.             End Try
  130.             Return db
  131.         End Function
  132.  
  133.         Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of CSize) Implements System.Collections.Generic.IEnumerable(Of CSize).GetEnumerator
  134.             Return L.GetEnumerator()
  135.         End Function
  136.         Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
  137.             Return L.GetEnumerator()
  138.         End Function
  139.     End Class

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам