J#集合到VB .NET集合

时间:2010-12-22 20:10:21

标签: vb.net collections ienumerator j#

我有一系列返回J#数据类型的API调用。我已经能够很好地转换大多数数据类型(Integer,Boolean,Double,Float等)。

我现在需要做的是将java.Util.Collection转换为VB .NET集合(ArrayList?)

这是我的尝试:

Public Function MakeDotNETCollection(ByVal javaCol As java.util.Collection) As Collection

    Dim dotNetCol As Collection

    If Not javaCol Is Nothing Then
      dotNetCol = New Collection

      Dim it As IEnumerator = javaCol.iterator()

      For Each it In CType(javaCol, Collection)
        dotNetCol.Add(it.Current)
      Next it

    End If

    Return dotNetCol
  End Function

我不断收到运行时错误“无法将'AbstractListlistIterator'类型的对象强制转换为'System.Collections.IEnumerator。有什么想法?

1 个答案:

答案 0 :(得分:1)

未经测试,但应该有效:

Public Function MakeDotNETCollection(ByVal javaCol As java.util.Collection) As Collection

  Dim dotNetCol As Collection

  If Not javaCol Is Nothing Then
    dotNetCol = New Collection

    Dim it As java.util.Iterator = javaCol.iterator()

    While it.HasNext()
      dotNetCol.Add(it.next())
    End While

  End If

  Return dotNetCol

End Function