引用程序集中的扩展方法?
如果我尝试调用定义如下的扩展方法:
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
通过调用
Dim sss As String()
sss.AsEnumerable.ToSortableBindingList()
它会给出错误“ToSortableBindingList 不是 System.Collections.Generic.IEnumerable(Of String) 的成员”。
注意:智能感知会在最后一个周期后自动完成!如果我尝试调用 context.TestTable.AsEnumerable.ToSortableBindingList (TestTable 是纯 EF4 生成的类),它甚至不会显示智能感知。我不明白为什么。扩展方法声明“ByVal source As IEnumerable(Of TSource)”有什么问题?
*********************************** 编辑 ************** ******************
好的,为了澄清正在发生的事情,我想提供一些额外的信息。 我可以将问题追踪到以下情况:
场景:
Assembly1(根命名空间“myapp”):
...
<System.Runtime.CompilerServices.Extension()> _
Public Function ToTest(ByVal source As String) As String
Return ""
End Function
...
'调用作品:
...
Dim a as string
a.ToTest()
...
Assembly2: (包括对 Assembly1 的引用)
'调用不起作用:
imports myapp
...
Dim a as string
a.ToTest()
If I try to call my extension method which is defined like this:
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
by calling
Dim sss As String()
sss.AsEnumerable.ToSortableBindingList()
it gives an error "ToSortableBindingList is not a member of System.Collections.Generic.IEnumerable(Of String)".
Note: Intellisense autocompletes after the last period! If I try to call context.TestTable.AsEnumerable.ToSortableBindingList (TestTable is a pure EF4 generated class) it not even shows up with intellisense. I don't get why. What's wrong with the extension method declaration "ByVal source As IEnumerable(Of TSource)"?
*********************************** EDIT ********************************
Ok, to clarify what's happening I'd like to provide some additional info.
I can track down the problem to the following:
Scenario:
Assembly1 (root namespace "myapp"):
...
<System.Runtime.CompilerServices.Extension()> _
Public Function ToTest(ByVal source As String) As String
Return ""
End Function
...
'Calling works:
...
Dim a as string
a.ToTest()
...
Assembly2:
(Reference to Assembly1 included)
'Calling does not work:
imports myapp
...
Dim a as string
a.ToTest()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的命名空间“myapp”不能直接包含函数“ToTest”,那里定义了一个模块。
尝试
并确保它是公共模块
Your namespace "myapp" cannot directly contain the function "ToTest", there is a Module defined there.
Try
and make sure it is a Public Module