use*_*253 10 .net vb.net extension-methods
如果我尝试调用我的扩展方法,其定义如下:
Module LinqExtensions
<System.Runtime.CompilerServices.Extension()> _
Public Function ToSortableBindingList(Of TSource)(ByVal source As IEnumerable(Of TSource)) As IBindingList
If (source Is Nothing) Then
Throw New ArgumentNullException("source")
End If
Return New SortableBindingList(Of TSource)(New List(Of TSource)(source))
End Function
End Module
Run Code Online (Sandbox Code Playgroud)
通过电话
Dim sss As String()
sss.AsEnumerable.ToSortableBindingList()
Run Code Online (Sandbox Code Playgroud)
它给出了一个错误"ToSortableBindingList不是System.Collections.Generic.IEnumerable(Of String)的成员".
注意:智能感知在最后一段时间后自动完成!如果我尝试调用context.TestTable.AsEnumerable.ToSortableBindingList(TestTable是一个纯EF4生成的类),它甚至不会显示intellisense.我不明白为什么.扩展方法声明"ByVal source As IEnumerable(Of TSource)"有什么问题?
好的,为了澄清发生了什么,我想提供一些额外的信息.我可以追踪到以下问题:
场景:
Assembly1(根名称空间"myapp"):
...
<System.Runtime.CompilerServices.Extension()> _
Public Function ToTest(ByVal source As String) As String
Return ""
End Function
...
Run Code Online (Sandbox Code Playgroud)
'来电作品:
...
Dim a as string
a.ToTest()
...
Run Code Online (Sandbox Code Playgroud)
Assembly2 :(包括对Assembly1的参考)
'呼叫不起作用:
imports myapp
...
Dim a as string
a.ToTest()
Run Code Online (Sandbox Code Playgroud)
Wil*_*lem 18
Your namespace "myapp" cannot directly contain the function "ToTest", there is a Module defined there. Try
Imports myapp.LinqExtensions
Run Code Online (Sandbox Code Playgroud)
and make sure it is a Public Module