I'm trying to write a method to return a list of all of a particular type of control:
So I have something like the following method:
<!-- language: lang-vb -->Private Function GetAllChildControls(Of T)(ByVal grid As DataGridItemCollection)
As List(Of T)
Dim childControls As New List(Of T)
For Each item As DataGridItem In grid
For Each tableCell As TableCell In item.Cells
If tableCell.HasControls Then
For Each tableCellControl As Control In tableCell.Controls
If tableCellControl.GetType Is GetType(T) Then
childControls.Add(DirectCast(tableCellControl, T))
End If
Next
End If
Next
Next
Return childControls
End Function
But this fails on the following code:
<!-- language: lang-vb -->childControls.Add(DirectCast(tableCellControl, T))
I get the message:
Cannot cast expression of type
System.Web.UI.Control
to typeT
.
How can I return a type specific list?