Is it possible to implement extension methods on generic interfaced collections in VB.NET?

Viewed 120

I can't manage to write a VB.NET extension method which can be called from an object of type ListOfTests in the below solution.

I can write the extension method in C# using generic type constraints.

Here's some test code I've writen to demonstrate the problem which I have encountered in a much larger existing VB.NET solution.

The extension method in question here is FilterByIDs, which is just an abritrary example I chose.

Working C# solution:

using System;
using System.Collections.Generic;
using System.Linq;

static class TestApp
{
    public static void Main()
    {
        var objTest = new ListOfTests
        {
            new Test {Id = 1}, new Test {Id = 2}, new Test {Id = 3}
        };

        objTest.FilterByIDs(new [] {3});

        // Prints "3" only - Extension method works
        objTest.ForEach(x => Console.WriteLine(x.Id));
    }
}

public static class Extensions
{
    public static void FilterByIDs<T, TID>(this List<T> target, IEnumerable<TID> objIDs) where T : ITest<TID>
    {
        target.RemoveAll(x => !objIDs.Contains(x.Id));
    }
}

public class ListOfTests : List<Test>
{}


public class Test : ITest<int>
{
    public int Id { get; set; }
}

public interface ITest<TID>
{
    TID Id { get; }
}

My attempt at writing this in VB.NET (Does not build):

Imports System.Runtime.CompilerServices

Class TestApp
    Public Shared Sub Main()
        Dim objTest As New ListOfTests()
        objTest.Add(New Test() With { .ID = 1})
        objTest.Add(New Test() With { .ID = 2})
        objTest.Add(New Test() With { .ID = 3})

        objTest.FilterByIDs({3})

        objTest.ForEach(Sub(x) Console.WriteLine(x.ID))
    End Sub
End Class

Public Module Extensions
    <Extension>
    Public Sub FilterByIDs(Of T As ITest(Of TID), TID)(target As List(Of T), objIDs As IEnumerable(Of TID))
        target.RemoveAll(Function(x) Not objIDs.Contains(x.Id))
    End Sub
End Module

Public Class ListOfTests
    Inherits List(Of Test)
End Class

Public Class Test
    Implements ITest(Of Integer)

    Public Property ID As Integer Implements ITest(Of Integer).ID
End Class

Public Interface ITest (Of T)

    Property ID As T
End Interface

Build error from the VB.NET solution is

" TestApp.vb(18, 16): [BC36561] Extension method 'FilterByIDs' has type constraints that can never be satisfied."

Is it possible to write this in VB.NET?

1 Answers

Not the same elegance and compile safety, but working:

<Extension>
Public Module Extensions

    <Extension>
    Public Sub FilterByIDs(Of T)(target As IList, objIDs As IEnumerable(Of T))
        If (target Is Nothing) Then Throw New NullReferenceException() 'To simulate instance method behavior
        For i As Int32 = target.Count - 1 To 0 Step -1
            Dim myTest As ITest(Of T) = TryCast(target(i), ITest(Of T))
            If (myTest Is Nothing) Then
                target.RemoveAt(i)
            Else
                If (Not objIDs.Contains(myTest.ID)) Then target.RemoveAt(i)
            End If
        Next
    End Sub

End Module
Related