Extension Method (
Extension methods) Is a static method that can be called through the syntax of the instance method. In terms of the final effect, the extension method makes it a reality to expand an existing type and construct a type with an additional method. (Note: Only 3.5 is available)
1 Imports system. runtime. compilerservices
2 ''' <summary>
3 ''' even extension class
4 ''' </Summary>
5 ''' <remarks> </remarks>
6 Module myextension
7''' <summary>
8 ''' returns a string of a certain length.
9 ''' </Summary>
10''' <Param name = "str"> input string </param>
11''' <Param name = "len"> return length </param>
12''' <returns> </returns>
13 ''' <remarks> </remarks>
14 <extension ()> _
15 public function getstring (byval STR as string, byval Len as integer) as string
16 return left (STR, Len)
17 end Function
18end Module
Call:
1 module module1
2
3 sub main ()
4 dim STR as string = ""
5 console. writeline (Str. getstring (12 ))
6 end sub
7
8end Module
The extended method getstring is added to the string class.
Running effect: