vb.net泛型得写法

Imports System.IO
Imports System.Xml.Serialization

Public Module Module1

    Sub Main()
        Dim cc As clsCustomConfig = New clsCustomConfig()
        cc.CellConfigList = New List(Of clsCellConfig)
        cc.CellConfigList.Add(New clsCellConfig() With {.CellID = 1, .RsrpMax = 100, .RsrpMin = 90})
        cc.CellConfigList.Add(New clsCellConfig() With {.CellID = 2, .RsrpMax = 100, .RsrpMin = 90})
        cc.CellConfigList.Add(New clsCellConfig() With {.CellID = 3, .RsrpMax = 100, .RsrpMin = 90})

        cc.Data_Enable = True
        cc.MO_Enable = True
        cc.MT_Enanble = True
        cc.StepNumber = 1

        Dim filePath = Environment.CurrentDirectory + "\test.xml"

        If SaveConfigToFile(Of clsCustomConfig)(filePath, cc) Then
            Console.WriteLine("Save successfully")
        End If

        Dim temp = New clsCustomConfig
        If ReadConfigFromFile(Of clsCustomConfig)(filePath, temp) Then
            Console.WriteLine("read successfully")
        End If
        Console.Read()

    End Sub


    Public Class clsCellConfig
        Public Property CellID As Integer

        Public Property RsrpMax As Double
        Public Property RsrpMin As Double
    End Class


    Public Class clsCustomConfig
        Public Property CellConfigList As List(Of clsCellConfig)
        Public Property Data_Enable As Boolean
        Public Property MO_Enable As Boolean
        Public Property MT_Enanble As Boolean
        Public Property StepNumber As Integer


    End Class

    Public Function SaveConfigToFile(Of T)(ByVal strFilePath As String, ByVal data As T) As Boolean
        Dim objStreamWriter As New StreamWriter(strFilePath)
        Try
            Dim xsSerialize As New XmlSerializer(data.GetType)
            xsSerialize.Serialize(objStreamWriter, data)
            objStreamWriter.Close()
        Catch ex As Exception
            objStreamWriter.Close()
            Return False
        End Try
        Return True
    End Function

    Public Function ReadConfigFromFile(Of T)(ByVal strFilePath As String, ByRef data As T) As Boolean
        Dim objStreamReader As New StreamReader(strFilePath)
        Try
            Dim xsDeserialize As New XmlSerializer(data.GetType)
            data = xsDeserialize.Deserialize(objStreamReader)
            objStreamReader.Close()
        Catch ex As Exception
            objStreamReader.Close()
            Return False
        End Try

        Return True
    End Function

End Module

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值