Using Extension Methods in VS2005
-
Hi, I know that you can use extension methods in C# while targeting version 2.0 of the .NET framework by creating your own System.Runtime.CompilerServices.ExtensionAttribute I've done the same in a VB.NET application but for the life of me I can't get the thing to work. Here is the code I'm using:
Namespace System.Runtime.CompilerServices
<attributeusage(attributetargets.method> _
Public NotInheritable Class ExtensionAttribute
Inherits Attribute
End Class
End NamespaceIn a module
Imports System.Runtime.CompilerServices
<extension()> _
Module ExtensionMethods
<extension()> _
Public Function Test(ByVal s As String) As String
Return s & " hello"
End Function
End ModuleSub Main()
Dim s As String = "Hello"
Debug.Print(s.Test())
End SubI get a compiler error saying that "Test is not a member of String" Am I missing something or can you simply not use extension methods in VB.NET for version 2.0? Thanks very much, dlarkin77
-
Hi, I know that you can use extension methods in C# while targeting version 2.0 of the .NET framework by creating your own System.Runtime.CompilerServices.ExtensionAttribute I've done the same in a VB.NET application but for the life of me I can't get the thing to work. Here is the code I'm using:
Namespace System.Runtime.CompilerServices
<attributeusage(attributetargets.method> _
Public NotInheritable Class ExtensionAttribute
Inherits Attribute
End Class
End NamespaceIn a module
Imports System.Runtime.CompilerServices
<extension()> _
Module ExtensionMethods
<extension()> _
Public Function Test(ByVal s As String) As String
Return s & " hello"
End Function
End ModuleSub Main()
Dim s As String = "Hello"
Debug.Print(s.Test())
End SubI get a compiler error saying that "Test is not a member of String" Am I missing something or can you simply not use extension methods in VB.NET for version 2.0? Thanks very much, dlarkin77
If I remember correctly, extension methods cannot be targeted for version 2 (for example extension attribute was introduced in 3.5). Have a look at this article: Extension Methods (Visual Basic)[^].
The need to optimize rises from a bad design. My articles[^]
-
Hi, I know that you can use extension methods in C# while targeting version 2.0 of the .NET framework by creating your own System.Runtime.CompilerServices.ExtensionAttribute I've done the same in a VB.NET application but for the life of me I can't get the thing to work. Here is the code I'm using:
Namespace System.Runtime.CompilerServices
<attributeusage(attributetargets.method> _
Public NotInheritable Class ExtensionAttribute
Inherits Attribute
End Class
End NamespaceIn a module
Imports System.Runtime.CompilerServices
<extension()> _
Module ExtensionMethods
<extension()> _
Public Function Test(ByVal s As String) As String
Return s & " hello"
End Function
End ModuleSub Main()
Dim s As String = "Hello"
Debug.Print(s.Test())
End SubI get a compiler error saying that "Test is not a member of String" Am I missing something or can you simply not use extension methods in VB.NET for version 2.0? Thanks very much, dlarkin77
It seems I was being particularly dense yesterday afternoon. The reason I couldn't get this to work was that I was actually using VS 2005 instead of VS 2008. The above code snippets work correctly when targeting .NET 2.0 in a VS 2008 project Thanks, dlarkin77