-1

我需要将一个强类型的类集合 List 传递给一个函数,并且需要遍历该类集合的属性,并且需要检查这些属性值字符串是否包含任何非法 XML 字符。请给我一个示例如何处理上述情况。

4

2 回答 2

1

有很多方法可以做到这一点

要么使用对象列表——</p>

Dim T As List(Of mClass) = New List(Of mClass)
MyFunction(T)

Dim T2 As List(Of mClass2) = New List(Of mClass2)
MyFunction(T2)


Private Sub MyFunction(ByVal myList As List(Of Object))
    For Each MyCls In myList
        If TypeOf MyCls Is mClass Then
            Dim cInst As mClass = DirectCast(MyCls, mClass)
            cInst.P = 
        ElseIf TypeOf MyCls Is mClass2 Then
            Dim cInst As mClass2 = DirectCast(MyCls, mClass2)
            cInst.A = 
        End If
    Next
End Sub

或者只是将列表作为对象传递-

Dim T As List(Of mClass) = New List(Of mClass)
MyFunction(T)

Dim T2 As List(Of mClass2) = New List(Of mClass2)
MyFunction(T2)


Private Sub MyFunction(ByVal myList As Object)
    If TypeOf myList Is List(Of mClass) Then
        Dim cInst As List(Of mClass) = DirectCast(myList, List(Of mClass))
        For Each MyCls As mClass In cInst
            MyCls.P = 
        Next
    ElseIf TypeOf myList Is List(Of mClass2) Then
        Dim cInst As List(Of mClass2) = DirectCast(myList, List(Of mClass2))
        For Each MyCls As mClass2 In cInst
            MyCls.A = 
        Next
    End If
End Sub

这可能会有所帮助的另一件事如果您愿意,您可以为 Para 定义变量数据类型

Dim T As Integer
Dim T2 As String
MyFunction(Of Integer)(T)
MyFunction(Of String)(T2)

Private Sub MyFunction(Of AnyNameForType)(ByVal myList As AnyNameForType)

End Sub
于 2012-10-25T05:31:23.227 回答
0
Dim T As List(Of mClass) = New List(Of mClass)
myfunction(T)


Private Function MyFunction(ByVal myList As List(Of mClass))
    For Each MyCls As mClass In myList
        MyCls.mProperty = 
    Next
End Function
于 2012-10-22T13:31:26.030 回答