.net 将列表(HtmlElement)转换为HtmlElementCollection类

.net 将列表(HtmlElement)转换为HtmlElementCollection类,.net,vb.net,winforms,.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

这是我的密码

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
它将返回我
列表(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.Htmlement)”类型的值无法转换为“System.Windows.Forms.HtmlementCollection”


HtmlElementCollection
ICollection
Add()
IEnumerable
也可以


我猜接受元素的构造函数是
内部的
。因此,此集合将被视为只读。

尝试使用hecFilter.Cast()尝试使用
TryCast(hecFilter,HtmlElementCollection)
其他任何解决方案或suggestion@vikas:为什么需要返回集合而不是列表?我想要一个返回类型为
HtmlElementCollection
where
HtmlElementCollection
作为参数的函数。我想筛选
HtmlElementCollection
actually@vikas:您可以筛选
列表
,是吗?或者您想重新分配集合?如何进行筛选,我使用的是framework 2.0,它在2.0中存在吗?