VB2008新特性

1.扩展方法 (Extension Methods)

Person类扩展Print方法

Public Module PersonExtension
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub Print(ByVal aPerson As Person)
        If aPerson IsNot Nothing Then
            Console.WriteLine("Istance of Person : " & aPerson.ToString())
        Else
            Console.WriteLine("No instance of Person.")
        End If
    End Sub
End Module
Public Class Person
    Private m_Name As String
    Public Property Name() As String
        Get
            Name = m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property
    Public Overrides Function ToString() As String
        ToString = Name
    End Function
End Class
Public Class Student : Inherits Person
    Private m_ClassGroup As String
    Public Property ClassGroup() As String
        Get
            ClassGroup = m_ClassGroup
        End Get
        Set(ByVal value As String)
            m_ClassGroup = value
        End Set
    End Property
    Public Overrides Function ToString() As String
        ToString = Name & " (" & ClassGroup & ")"
    End Function
End Class
Public Class Example1
    Public Shared Sub Main()
        Dim person1 As Person = New Person With {.Name = "John"}
        Dim person2 As Person = New Student _
                                With {.Name = "Jane", _
                                      .ClassGroup = "Visual Basic .NET"}
        Dim person3 As Person
        '
        person1.Print()                                                    ' (1)
        person2.Print()
        person3.Print()
        '
        Console.ReadLine()
    End Sub
End Class

Output

 Istance of Person : John
 Istance of Person : Jane (Visual Basic .NET)
 No instance of Person.

对接口进行扩展

Public Interface SomeInterface
    Sub SomeFirstMethod()
End Interface
Public Module SomeInterfaceExtension
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub SomeSecondMethod(ByVal aSomeInterface As SomeInterface)
        Console.WriteLine("SomeInterface.SomeSecondMethod() implementation.")
    End Sub
End Module
Public Class SomeClass : Implements SomeInterface
    Public Sub SomeFirstMethod() Implements SomeInterface.SomeFirstMethod
        Console.WriteLine("SomeClass.SomeFirstMethod() implementation.")
    End Sub
End Class
Public Class Example3
    Public Shared Sub Main()
        Dim object1 As New SomeClass
        object1.SomeFirstMethod()
        object1.SomeSecondMethod()
        '
        Console.ReadLine()
    End Sub
End Class

一次扩展到哦个对象

Public Interface Interface1
End Interface
Public Interface Interface2
End Interface
Public Module InterfaceExtensions
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub Method1(ByVal aInterface1 As Interface1)
        Console.WriteLine("Interface1.Method1")
    End Sub
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub Method2(ByVal aInterface2 As Interface2)
        Console.WriteLine("Interface2.Method2")
    End Sub
End Module
Public Class Class1 : Implements Interface1, Interface2
End Class
Public Class Example4
    Public Shared Sub Main()
        Dim object1 As New Class1
        object1.Method1()
        object1.Method2()
        '
        Console.ReadLine()
    End Sub
End Class

2.对象初始化器(Object Initializers)

用New进行初始化

Class Person
    Private m_Name As String
    Public Property Name() As String
        Get
            Name = m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property
    Private m_Address As Address
    Public Property Address() As Address
        Get
            Address = m_Address
        End Get
        Set(ByVal value As Address)
            m_Address = value
        End Set
    End Property
End Class
Class Address
    Private m_Street As String
    Public Property Street() As String
        Get
            Street = m_Street
        End Get
        Set(ByVal value As String)
            m_Street = value
        End Set
    End Property
    Private m_City As String
    Public Property City() As String
        Get
            City = m_City
        End Get
        Set(ByVal value As String)
            m_City = value
        End Set
    End Property
    Private m_ZipCode As String
    Public Property ZipCode() As String
        Get
            ZipCode = m_ZipCode
        End Get
        Set(ByVal value As String)
            m_ZipCode = value
        End Set
    End Property
End Class
Class Counter
    Public Sub New(ByVal value As Integer)
        m_Value = value
    End Sub
    Private m_Value As Integer
    Public ReadOnly Property Value() As Integer
        Get
            Value = m_Value
        End Get
    End Property
    Private m_StepValue As Integer
    Public Property StepValue() As Integer
        Get
            StepValue = m_StepValue
        End Get
        Set(ByVal value As Integer)
            m_StepValue = value
        End Set
    End Property
    Public Sub Raise()
        m_Value += StepValue
    End Sub
End Class
Class Example
    Public Shared Sub Main()
        Dim person1 As Person = New Person() With {.Name = "John"}
        Console.WriteLine(person1.Name)
        '
        Dim person2 As Person = New Person()                                ' (1)
        With person2                                                        ' (1)
            .Name = "Paul"                                                  ' (1)
        End With                                                            ' (1)
        '
        Dim person3 As Person = New Person() _
                                With {.Name = "Jane", _
                                      .Address = New Address() _
                                                 With {.City = "New York"}} ' (2)
        Console.WriteLine(person3.Name)
        Console.WriteLine(person3.Address.City)
        '
        Dim counter1 As Counter = New Counter(5) With {.StepValue = 4}      ' (3)
        Console.WriteLine(counter1.Value)
        counter1.Raise()
        Console.WriteLine(counter1.Value)
        '
        Console.ReadLine()
    End Sub
End Class

 

3.匿名类型(Anonymous Types)

用With关键字进行类属性的定义和赋值

Option Infer On
Option Strict On
Class Example1
    Public Shared Sub Main()
        Dim address1 = New With {.City = "New York", .Street = "Parklane"}
        Console.WriteLine(address1.City)
        Console.WriteLine(address1.Street)
        '
        Dim address2 = New With {.City = "London", .Street = "Oxford Street"}
        Console.WriteLine(address2.City)
        Console.WriteLine(address2.Street)
        '
        Dim address3 = New With {.City = "San Fransico", .Street = 10}
        Console.WriteLine(address3.City)
        Console.WriteLine(address3.Street)
        '
        Dim address4 = New With {.City = "Paris"}
        Console.WriteLine(address4.City)
        '
        Dim city As String = "Brussels"
        Dim address5 = New With {city}                                     ' (1)
        Console.WriteLine(address5.City)
        address5.City = "Amsterdam"
        Console.WriteLine(address5.City)
        '
        Dim someInstance As New SomeFirstClass
        Dim address6 = New With {someInstance.City, someInstance.Street()}
        Console.WriteLine(address6.City)
        Console.WriteLine(address6.Street)
        '
        Dim address7 = New With {someInstance.City, someInstance.Street(), _
                                 someInstance.Number}                      ' (2)
        Console.WriteLine(address7.City)
        Console.WriteLine(address7.Street)
        Console.WriteLine(address7.Number)
        '
        Dim address8 = New With {SomeSecondClass.City, SomeSecondClass.Street, _
                                 SomeSecondClass.Number}                   ' (3)
        Console.WriteLine(address8.City)
        Console.WriteLine(address8.Street)
        Console.WriteLine(address8.Number)
        '
        Console.WriteLine(address1.GetType().Equals(address2.GetType()))
        Console.WriteLine(address1.GetType().Equals(address3.GetType()))
        Console.WriteLine(address1.GetType().Equals(address4.GetType()))
        Console.WriteLine(address4.GetType().Equals(address5.GetType()))
        Console.WriteLine(address1.GetType().Equals(address6.GetType()))
        '
        Console.ReadLine()
    End Sub
End Class
Class SomeFirstClass
    Public Number As Integer
    Public ReadOnly Property City() As String
        Get
            City = "someCity"
        End Get
    End Property
    Public Function Street() As String
        Street = "someStreet"
    End Function
End Class
Class SomeSecondClass
    Public Shared Number As Integer
    Public Shared ReadOnly Property City() As String
        Get
            City = "someCity"
        End Get
    End Property
    Public Shared Function Street() As String
        Street = "someStreet"
    End Function
End Class

 

4.局部方法 (Partial Methods)

'定义局部方法 需要用 Partial 做修饰符
'局部方法不一定总是有执行内容的,也就是说定义的方法 可以一句操作语句都没有
'局部方法不可以有返回值
'局部方法可以是静态(Shared)方法
'局部方法可以包含参数,参数可以包含以下修饰词:ByRef,Optional
'局部方法必须是私有(Private)方法

Class SomeClass
    Partial Private Sub somePartialMethod1()
    End Sub
    Partial Private Sub somePartialMethod2()
    End Sub
    '
    Private Sub somePartialMethod1()                                       ' (1)
        Console.WriteLine("somePartialMethod1")
    End Sub
    '
    Public Sub TestSomePartialMethod1()
        somePartialMethod1()
    End Sub
    Public Sub TestSomePartialMethod2()
        somePartialMethod2()
    End Sub
End Class
Partial Class SomeClass
    Private Sub somePartialMethod2()                                       ' (2)
        Console.WriteLine("somePartialMethod2")
    End Sub
End Class
Class Example1
    Public Shared Sub Main()
        Dim someObject As New SomeClass
        someObject.TestSomePartialMethod1()
        someObject.TestSomePartialMethod2()
        '
        Console.ReadLine()
    End Sub
End Class

 

5.Lambda表达式

Delegate实例可以指向静态方法

Option Infer On
Option Strict On
Public Class Example1
    Public Delegate Function SomeDelegate(ByVal value As Integer) As Integer
    Public Shared Sub Main()
        Dim value1 As Integer
        '
        Dim delegate1 As SomeDelegate = _
                                  New SomeDelegate(AddressOf SomeFunction) ' (1)
        value1 = delegate1.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate2 As SomeDelegate = AddressOf SomeFunction             ' (2)
        value1 = delegate2.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate3 = New SomeDelegate(AddressOf SomeFunction)           ' (3)
        value1 = delegate3.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate4 As Func(Of Integer, Integer) = _
                     New Func(Of Integer, Integer)(AddressOf SomeFunction) ' (4)
        value1 = delegate4.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate5 As Func(Of Integer, Integer) = _
                                                    AddressOf SomeFunction ' (5)
        value1 = delegate5.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate6 = _
                     New Func(Of Integer, Integer)(AddressOf SomeFunction) ' (6)
        value1 = delegate6.Invoke(5)
        Console.WriteLine(value1)
        '
        Console.ReadLine()
    End Sub
    Public Shared Function SomeFunction(ByVal value As Integer) As Integer
        SomeFunction = value * 2
    End Function
End Class

通过Lambda定义完整的方法

Public Class Example2
    Public Shared Sub Main()
        Dim value1 As Integer
        '
        Dim delegate1 As Example1.SomeDelegate = _
                                      Function(value As Integer) value * 2 ' (1)
        value1 = delegate1.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate2 As Func(Of Integer, Integer) = _
                                      Function(value As Integer) value * 2 ' (2)
        value1 = delegate2.Invoke(5)
        Console.WriteLine(value1)
        '
        Dim delegate3 = Function(value As Integer) value * 2               ' (3)
        value1 = delegate3.Invoke(5)
        Console.WriteLine(value1)
        '
        value1 = (Function(value As Integer) value * 2).Invoke(5)
        Console.WriteLine(value1)
        '
        Console.WriteLine((Function(value As Integer) value * 2).Invoke(5))
        '
        Console.ReadLine()
    End Sub
End Class

对Lambd表达式中嵌入Lambd表达式

Public Class Example4
    Public Shared Sub Main()
        Dim delegate1 = Function(arg1 As Integer) _
                            Function(arg2 As Integer) arg1 + arg2
        Dim value1 As Integer = delegate1.Invoke(2).Invoke(3)
        Console.WriteLine(value1)
        '
        Console.ReadLine()
    End Sub
End Class

 

6.语言集成查询 (Lambda or Inline Functions)

以下为LinQ to Object的例子。

从persons查找"New York",并按Name排序

Option Infer On
Option Strict On
Public Class Person
    Private m_Name As String
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property
    Private m_City As String
    Public Property City() As String
        Get
            Return m_City
        End Get
        Set(ByVal value As String)
            m_City = value
        End Set
    End Property
    Private m_IsMale As Boolean
    Public Property IsMale() As Boolean
        Get
            Return m_IsMale
        End Get
        Set(ByVal value As Boolean)
            m_IsMale = value
        End Set
    End Property
    Public Overrides Function ToString() As String
        ToString = "Female "
        If IsMale Then ToString = "Male "
        ToString &= Name & ", from " & City & "."
    End Function
End Class
Public Class Example1
    Public Shared Sub Main()
        Dim persons As Person() = New Person() { _
          New Person With {.Name = "John", .City = "New York", .IsMale = True}, _
          New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _
          New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _
          New Person With {.Name = "Paul", .City = "New York", .IsMale = True}}
        '
        Dim personsNY As IEnumerable(Of Person) = _
                     persons.Where(Function(person) person.City = "New York") _
                            .OrderBy(Function(person) person.Name) _
                            .Select(Function(person) person)
        '
        For Each person As Person In personsNY
            Console.WriteLine(person)
        Next
        '
        Console.ReadLine()
    End Sub
End Class

利用LinQ表达式

Public Class Example2
    Public Shared Sub Main()
        Dim persons As Person() = New Person() { _
          New Person With {.Name = "John", .City = "New York", .IsMale = True}, _
          New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _
          New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _
          New Person With {.Name = "Paul", .City = "New York", .IsMale = True}}
        '
        Dim personsNY As IEnumerable(Of Person) = _
                                                From person In persons _
                                                Where person.City = "New York" _
                                                Order By person.Name _
                                                Select person
        '
        For Each person As Person In personsNY
            Console.WriteLine(person)
        Next
        '
        Console.ReadLine()
    End Sub
End Class