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

I have an outdated VB.Net class library that includes some extension methods, one of which is something like 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 need to import Extensions.OrmExtensions. I have another project that is in C #, which depends on VB with extensions, and I cannot get them to work. OrmExtensions is not available and just uses the VBProject.Extensions namespace does not make extensions available.

There are several projects that depend on the VB project, and, ideally, extensions should be available for all of them.

I did quite a lot of search queries, but could not tell anything about how to use VB extension methods in C #. I assume the problem is that they should be in the module, but I could not confirm this.

I would prefer not to duplicate extension methods wherever they are needed (especially in our unit test project, which is in C #).

+6
source share
1 answer

You cannot use ByRef .

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

Parameter

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

+5
source

Source: https://habr.com/ru/post/944553/


All Articles