Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 925
user449253 Avatar asked Sep 16 '10 08:09

user449253


People also ask

What are extension methods in agriculture?

Extension methods comprise the communication techniques between extension workers and target groups. To facilitate farmers' decisions whether or not and how to adopt fish farming. the problems to be solved. Generally speaking, mass media help extension agents to reach large numbers of farmers simultaneously.

What are the extension methods in C#?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

What is extension method in MVC?

What is extension method? Extension methods in C# are methods applied to some existing class and they look like regular instance methods. This way we can "extend" existing classes we cannot change. Perhaps the best example of extension methods are HtmlHelper extensions used in ASP.NET MVC.

Can you add extension methods to an existing static class?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.


1 Answers

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

like image 199
Willem Avatar answered Sep 23 '22 22:09

Willem