Search code examples
vb.netvisual-studio-2013extension-methodsprojects

Create a module of Extensions methods for all the new projects


I'd like to know if there is a way to create a module of Extensions methods for all the new projects in Visual Studio 2013.

For example, this is my Module :

Imports System.Runtime.CompilerServices
Imports System.IO

Module Extensions

    <Extension> Public Function ReplaceFirst(value As String, oldValue As String, newValue As String) As String
        Dim position As Integer = value.IndexOf(oldValue)
        If position = -1 Then
            Return value
        Else
            Return value.Substring(0, position) + newValue + value.Substring(position + oldValue.Length)
        End If
    End Function

    <Extension> Public Function ReadAllLines(value As String) As List(Of String)
        If value Is Nothing Then
            Return Nothing
        End If

        Dim lines As New List(Of String)
        Using StringRdr As New StringReader(value)
            While StringRdr.Peek() <> -1
                Dim line As String = StringRdr.ReadLine
                If Not String.IsNullOrWhiteSpace(line) Then
                    lines.Add(line)
                End If
            End While
        End Using

        Return lines
    End Function

    <Extension> Public Function UppercaseFirstLetter(value As String) As String
        If String.IsNullOrEmpty(value) Then
            Return value
        End If

        Dim Chars() As Char = value.ToCharArray
        Chars(0) = Char.ToUpper(Chars(0))

        Return New String(Chars)
    End Function

    <Extension> Public Function ZeroBased(value) As Integer
        Return value - 1
    End Function

End Module

How can I use this Extensions methods in all my projects without adding the module in all of them ?

Regards, Drarig29.


Solution

  • Make it a library (DLL) and add a reference to the library in each project.