Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use VB.Net extension methods in a C# project

Tags:

c#

.net

vb.net

I have a legacy VB.Net class library that includes some extension methods, one of which is roughly this:

Namespace Extensions
    Public Module OrmExtensions

        <Extension()>
        Public Function ToDomainObjectCollection(ByRef objects As OrmCollection(Of OrmObject)) As DomainObjectCollection
            Return objects.AsQueryable().ToDomainObjectCollection()
        End Function

        <Extension()>
        Public Function ToDomainObjectCollection(ByRef objects As IQueryable(Of OrmObject)) As DomainObjectCollection
            Dim doc As New DomainObjectCollection()
            For Each o In objects
                doc.Add(o.ToDomainObject())
            Next
            Return doc
        End Function
    End Module
End Namespace

To use these extensions in VB I just have to import Extensions.OrmExtensions. I have another project, which is in C# that depends on the VB one with the extensions and I can't get them to work. OrmExtensions isn't available and simply using the namespace VBProject.Extensions doesn't make the extensions available.

There are multiple projects that depend on the VB project and ideally the extensions should be available to all of them.

I've done a fair bit of googling but haven't been able to turn up anything about actually using VB extension methods in C#. I assume the issue is that they're required to be in a Module, but I haven't been able to confirm that.

I'd rather not duplicate the extension methods everywhere they're required(especially in our unit test project, which is in C#).

like image 460
Justin Edwards Avatar asked May 08 '13 17:05

Justin Edwards


People also ask

Where we can use extension method in C#?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

What is extension method in VB net?

Extension methods enable developers to add custom functionality to data types that are already defined without creating a new derived type. Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.

How do you call an extension method?

To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.

Can you add extension methods to an existing static class?

An extension method must be defined in a top-level static class. An extension method with the same name and signature as an instance method will not be called. Extension methods cannot be used to override existing methods.


1 Answers

You should not be using ByRef.

VB.Net (apparently) supports extension methods on ref parameters; C# (rightfully) does not.

A ByRef (ref in C#) parameter allows the method to assign a new instance to the variable or field passed by the caller.
They should rarely be used.

like image 85
SLaks Avatar answered Oct 04 '22 02:10

SLaks