我试图用ConvertAll压平一个多维数组,但我无法让它工作:
Private Function Flatten(ByRef a As Object) As Object
Dim elements As Integer = 0
Dim size As Integer
For r = 0 To a.Rank - 1
size = a.GetUpperBound(r) - a.GetLowerBound(r) + 1
If elements = 0 Then
elements = size
Else
elements = elements * size
End If
Next
Dim result(elements - 1) As Object
' Fails !
result = a.ConvertAll(a, New Converter(Of Object, Object)(Function(x) x))
Return result
End Function
Sub Main()
Dim a(,) As Integer = {{1, 2, 3}, {4, 5, 6}}
Dim b(5) As Integer
b = Flatten(a)
End Sub
为什么会抛出NullReferenceException?
由于