是否可以在SortedList(Of String,Generic Type)中传递泛型类型

时间:2018-10-07 19:35:26

标签: vb.net structure sortedlist

我目前有几个StructureSortedList

 Public Structure Membres
    Dim NoMembre As String
    Dim TypeMembre As String
    Dim LangCode As String
    Dim MembreNom As String
    Dim MembrePrenom As String
    Dim MembreAdresse As String
    Dim MembreVille As String
    Dim ProvCode As String
    Dim MembreCodePostal As String
    Dim MembreNoTel As String
    Dim MembreEMail As String
End Structure

Public Structure Provinces
    Dim ProvCode As String
    Dim ProvDesc As String
End Structure

Public Structure Langues
    Dim LangCode As String
    Dim LangDesc As String
End Structure

Public Structure TypesMembres
    Dim TypeMembre As String
    Dim TypeMembreDesc As String
End Structure

Public FicheMembre As New SortedList(Of String, Membres)
Public FicheProvince As New SortedList(Of String, Provinces)
Public FicheLangue As New SortedList(Of String, Langues)
Public FicheTypeMembre As New SortedList(Of String, TypesMembres)

我想要实现的是将这些SortedList中的任何一个作为参数传递为通用Type形式:

Private Function readTxt(list As SortedList(Of String, Generic Type), path 
As String)

现在可以做到这一点,我怎么实现呢?

我已经尝试过传递ObjectType等。没有一个有效。也许我在这里想念什么?

这是我尝试过但失败的事情:

Private Function ReadTxt(Of T)(list As SortedList(Of String, T), path As String) As SortedList(Of String, T)

    Dim reader As System.IO.StreamReader
    reader = IO.File.OpenText(path)
    Dim str As String()
    Dim i As Integer = 0
    Do While reader.Peek <> -1
        Dim line As String = reader.ReadLine
        Dim key As String = line.Substring(0, line.IndexOf("|"))

        If Not (list.ContainsKey(key)) Then
            str = line.Split("|")
            list.Add(key, getMembre(str))
        End If
        i += 1
    Loop
    Return list
End Function

我对 ANSWER 的实施:

    Private Function GetLists(Str As String(), t1 As Object) As Object

    Dim m As Membres
    Dim p As Provinces
    Dim tm As TypesMembres
    Dim l As Langues

    Select Case True
        Case TypeOf t1 Is Membres
            m.NoMembre = Str(0)
            m.MembreNom = Str(3)
            m.MembrePrenom = Str(4)
            m.MembreAdresse = Str(5)
            m.MembreVille = Str(6)
            m.ProvCode = Str(7)
            m.MembreCodePostal = Str(8)
            m.MembreNoTel = Str(9)
            m.MembreEMail = Str(10)
            m.LangCode = Str(2)
            m.TypeMembre = Str(1)
            Return m
        Case TypeOf t1 Is Provinces
            p.ProvCode = Str(0)
            p.ProvDesc = Str(1)
            Return p
        Case TypeOf t1 Is TypesMembres
            tm.TypeMembre = Str(0)
            tm.TypeMembreDesc = Str(1)
            Return tm
        Case TypeOf t1 Is Langues
            l.LangCode = Str(0)
            l.LangDesc = Str(1)
            Return l
    End Select

End Function

Private Function ReadTxt(Of T)(list As SortedList(Of String, T), path As String, args As Object) As SortedList(Of String, T)
    Dim reader As System.IO.StreamReader
    reader = IO.File.OpenText(path)
    Dim str As String()
    Do While reader.Peek <> -1
        Dim line As String = reader.ReadLine
        Dim key As String = line.Substring(0, line.IndexOf("|"))

        If Not (list.ContainsKey(key)) Then
            str = line.Split("|")
            list.Add(key, GetLists(str, args))
        End If
    Loop
    Return list
End Function

Private Sub GetProvName()

End Sub

这对我有用,因为我只想知道作为参数传递的结构类型。

1 个答案:

答案 0 :(得分:2)

要使方法通用,您至少需要添加一个类型参数。 VB.NET中类型参数的语法为(Of T1, T2, ...)

因此,您的函数应如下所示:

Private Function ReadTxt(Of T)(list As SortedList(Of String, T), path As String) As String
    ' Your code here.
End Function

用法:

ReadTxt(FicheMembre, "SomeString")
ReadTxt(FicheProvince, "SomeString")
'
' etc.

参考: