Extension methods in referenced assemblies?
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()
Your namespace "myapp" cannot directly contain the function "ToTest", there is a Module defined there. Try
Imports myapp.LinqExtensions
and make sure it is a Public Module
精彩评论