我有一個函數,應該遞歸循環通過一個控件,它是子項並返回該控件中的任何表單值(文本框,複選框,單選按鈕),然後將其作爲字典吐出,其中的鍵是控件ID,該值是控件的文本或值。在遞歸函數中結合字典?
但是我遇到了在那裏遞歸發生線路的問題,它給了我下面的錯誤:
Unable to cast object of type '<UnionIterator>d__88
1[System.Collections.Generic.KeyValuePair2[System.String,System.String]]' to type 'System.Collections.Generic.Dictionary
2[System.String,System.String]'.`
代碼:
Public Shared Function getFormValuesInsideControl(ByVal control As Control) As Dictionary(Of String, String)
Dim formValues As New Dictionary(Of String, String)
If control IsNot Nothing Then
If control.HasControls = True Then
For Each childControl As Control In control.Controls
formValues = formValues.Union(getFormValuesInsideControl(childControl)) 'error happens here'
Next
Else
Select Case TypeName(control)
Case "TextBox"
Dim textbox As TextBox = control
formValues.Add(formatControlName(textbox.ID), textbox.Text)
Case "CheckBox"
Dim checkbox As CheckBox = control
If checkbox.Checked = True Then
formValues.Add(formatControlName(checkbox.ID), "Yes")
End If
Case "RadioButton"
Dim radioButton As RadioButton = control
If radioButton.Checked = True Then
formValues.Add(formatControlName(radioButton.ID), "Yes")
End If
End Select
End If
End If
Return formValues
End Function
我看到發生了什麼,但我不知道爲什麼或如何解決它。
我該如何將這些字典結合在這一行中?我在LINQ中看到過這樣做的例子,但由於遞歸方面的原因,我認爲我不能這樣做。