将List(Of HtmlElement)转换为HtmlElementCollection类

时间:2013-04-14 06:03:05

标签: .net vb.net winforms

这是我的代码..

Private Function GetElement(ByVal hec As ICollection, ByVal strAttName As String, ByVal strAttVal As String) As List(Of HtmlElement)
    Dim hecFilter As New List(Of HtmlElement)
    Dim str As String
    For Each El As HtmlElement In hec
        str = El.GetAttribute(strAttName)
        If (Not IsNothing(str) AndAlso str.Trim() = strAttVal) Then
            hecFilter.Add(El)
        End If
    Next
    Return hecFilter
End Function

它会返回List(Of HtmlElement)

现在我想将其转换为HtmlElementCollection class

尝试这样做

Private Function GetElement(ByVal hec As HtmlElementCollection, ByVal strAttName As String, ByVal strAttVal As String) As HtmlElementCollection
    Dim hecFilter As New List(Of HtmlElement)
    Dim str As String
    For Each El As HtmlElement In hec
        str = El.GetAttribute(strAttName)
        If (Not IsNothing(str) AndAlso str.Trim() = strAttVal) Then
            hecFilter.Add(El)
        End If
    Next
    Return TryCast(hecFilter, HtmlElementCollection)
End Function

显示错误:

  

类型'System.Collections.Generic.List(Of System.Windows.Forms.HtmlElement)'的值无法转换为'System.Windows.Forms.HtmlElementCollection'。

1 个答案:

答案 0 :(得分:0)

HtmlElementCollection implements ICollection doesn't have Add()IEnumerable doesn't have或者......

我猜接受元素的构造函数是internal。因此威胁这个集合为只读。