Extension methods in referenced assemblies?

我与影子孤独终老i 提交于 2019-11-30 14:56:07

问题


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()

回答1:


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



来源:https://stackoverflow.com/questions/3724891/extension-methods-in-referenced-assemblies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!