【问题标题】:Is it possible to pass a generic type in a SortedList(Of String, Generic Type)是否可以在 SortedList(Of String, Generic Type) 中传递泛型类型
【发布时间】:2018-10-07 19:35:25
【问题描述】:

我目前有几个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

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

【问题讨论】:

    标签: vb.net structure sortedlist


    【解决方案1】:

    要使方法泛型,您需要添加至少一个类型参数。 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.
    

    参考资料:

    【讨论】:

    • 我已经尝试过了,但它给我一个错误:Value of type 'Form1.Membres' cannot be converted to 'T'.
    • getMembre 函数是 Membres 的一个实例,并返回一个类型 Membres
    • 我在 getMembres(str) 函数中得到它,因为它返回一个成员结构类型
    • 好吧,如果你想让方法是泛型的,它需要一直是泛型的。您不能使用返回Membres 的函数,因为Add() 方法需要T 类型的对象,它可以是任何其他类型(与传递给ReadTxt 方法的类型参数相同) .最重要的是,您需要将您的 getMembre 函数替换为另一个返回 T 的通用函数。
    • @NewBie1234 你的Public Structure Membres 应该是一个class,它有一个无参数的构造函数,它实现了一个由其他类(ProvincesLangues 等)共享的Interface。然后,您的 getMembre() 方法将返回此接口类型显式实现 (Dim m as ICommonInterface = New Membres() with { (...) } (...) Return m)。您的泛型会执行以下操作:list.Add(key, CType(getMembre(str), T))
    猜你喜欢
    • 2021-03-27
    • 1970-01-01
    • 2020-01-19
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2013-03-01
    相关资源
    最近更新 更多