简体   繁体   中英

Create and use an extension in vb.net

I've got 2 parts to this question:

  1. How to get extension to work in vb.net
  2. How to call the lambda expression in vb.net

I want to implement an extension in VB.NET and while I've used them numerous times in C#; I can't see why this won't work for me.

Here is one of extension I need to call in C#

public static class EnumerableExtensions
{
    public static string ToSeparator<T, TU>(this IEnumerable<T> source, 
    Func<T, TU> func, string separator, bool endWithSeparator)
    {
        string separatedString = string.Join(separator, source.Select(x =>
        {
            var value = func(x);
            return value == null ? string.Empty : value.ToString();
        }));

        separatedString += (endWithSeparator ? separator : string.Empty);

        return separatedString;
    }
}

I've converted to vb.net (using DeveloperFusion), so I'm not sure how accurate this is first of all as I had to make a few changes manually:

Imports System.Linq
Imports System.Runtime.CompilerServices

Namespace [Shared].Extensions

  Module IEnumerableExtensions

    <Extension()> _
    Public Function ToSeparator(Of T, TU)(source As IEnumerable(Of T), 
    func As Func(Of T, TU), separator As String, endWithSeparator As Boolean) 
    As String

      Dim separatedString As String = String.Join(separator, source.Select(
      Function(x)
        Return If(x Is Nothing, String.Empty, x.ToString())
      End Function))

      separatedString += (If(endWithSeparator, separator, String.Empty))

      Return separatedString

    End Function

  End Module

End Namespace

This is created in a separate assembly in C# and it works just fine when calling this way:

var list = mylist.ToSeparator(m=>m.LastName, ",", false);

but in vb.net, it just will not show up in intellisense,

2) I'm not sure how to call the part of the function m=>m.Lastname in vb.net

Unfortunately, my share library is also written in vb.net so I cannot justify creating a separate assembly in c# to for these few extensions I need, so I really need this resolved in VB.NET

Am I doing anything wrong? Any ideas why it won't show up in intellisense in vb.net the way it does in c#?

I'm using .NET 4.0 and VS 2013.

Thanks.

UPDATE

Below is the updated extension that's correctly converted to VB.NET. I thought I'd update it as it might be useful to someone else.

<Extension()> _
Public Function ToSeparator(Of T, TU)(source As IEnumerable(Of T), 
func As Func(Of T, TU), separator As String, endWithSeparator As Boolean) As String

  Dim separatedString As String = String.Join(separator, source.[Select](
    Function(x)
      Dim value As TU = func(x)
      Return If(value Is Nothing, String.Empty, value.ToString())
    End Function))

  separatedString += (If(endWithSeparator, separator, String.Empty))

  Return separatedString

End Function

Using this with @DStanley's answer will work as expected.

1)in vb.net, it just will not show up in intellisense,

You probably just need to make your Module public. VB Modules are Friend by default (roughly equivalent to C#'s internal :

Public Module IEnumerableExtensions

2) I'm not sure how to call the part of the function m=>m.Lastname in vb.net

A quick search for "VB Lambda" would have yielded plenty of examples:

list = mylist.ToSeparator(Function(m) m.LastName, ",", False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM