VB.NET 扩展方法错误
''' <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.Generic.List(Of T)' '...Utils' 中定义的不是通用的 (或者没有自由类型参数)并且 所以不能有类型参数。
是das吗?
''' <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
usage
Dim buttonControl As New System.Windows.Forms.Button
Dim controls = buttonControl.ToList(Of System.Windows.Forms.Control)()
compile time error (on the last line)
Extension method 'Public Function
ToList() As
System.Collections.Generic.List(Of T)'
defined in '...Utils' is not generic
(or has no free type parameters) and
so cannot have type arguments.
Was is das?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
基本上你的返回类型是一个泛型(声明为 List (of T))。这里的函数声明做到了这一点,以便返回类型是正在扩展的类型的列表。
Try this:
Basically your return type was a generic (declared as List (of T)). The function decaration here does it so that the return type is a list of the type that is being extended.
试试这个。
Try this.