''' <summary>
''' Transforms an item to a list of single element containing this item.
''' '</summary>
<Extension()> _
Public Function ToList(Of T)(ByVal item As T) As List(Of T)
Dim tList As New List(Of T)
tList.Add(item)
Return tList
End Function
Dim buttonControl As New System.Windows.Forms.Button
Dim controls = buttonControl.ToList(Of System.Windows.Forms.Control)()
編譯時間錯誤(在最後一行)
擴展方法「公共功能 ToList()作爲 System.Collections中。在'... Utils'中定義的'Generic.List(Of T)' 不是通用的 (或沒有自由類型參數)和 ,因此不能有類型參數。
是das?
如果我只是buttonControl.ToList()一切正常。但是,如果我嘗試從'Button'獲取'Control'的列表,則會顯示錯誤。 – serhio 2011-03-07 10:33:37
我明白了。那麼在調用ToList(類似)之前將ButtonControl投射到控件上:(buttonControl as System.Windows.Forms.Control).ToList – 2011-03-07 10:39:32
用您的示例,錯誤更改: 'Type argument'System.Windows.Forms.Control '不繼承或實現約束類型'System.Collections.Generic.List(Of System.Windows.Forms.Button)'。' – serhio 2011-03-07 10:40:00