匿名类型定义 (Visual Basic)

为了响应匿名类型的实例的声明,编译器会创建一个新的类定义,其中包含该类型的指定属性。

编译器生成的代码

对于以下定义 product,编译器将创建一个新的类定义,其中包含属性 NamePrice以及 OnHand

' Variable product is an instance of an anonymous type.
Dim product = New With {Key .Name = "paperclips", Key .Price = 1.29, .OnHand = 24}

类定义包含类似于以下内容的属性定义。 请注意,键属性没有 Set 方法。 键属性的值是只读的。

Public Class $Anonymous1
    Private _name As String
    Private _price As Double
    Private _onHand As Integer
     Public ReadOnly Property Name() As String
        Get
            Return _name
        End Get
    End Property

    Public ReadOnly Property Price() As Double
        Get
            Return _price
        End Get
    End Property

    Public Property OnHand() As Integer
        Get
            Return _onHand
        End Get
        Set(ByVal Value As Integer)
            _onHand = Value
        End Set
    End Property

End Class

此外,匿名类型定义还包含无参数构造函数。 不允许需要参数的构造函数。

如果匿名类型声明至少包含一个键属性,则类型定义将覆盖继承自 Object 的三个成员:EqualsGetHashCodeToString。 如果未声明键属性,则仅替代 ToString。 替代提供以下功能:

  • Equals True如果两个匿名类型实例是同一个实例,或者它们满足以下条件,则返回:

    • 它们具有相同数量的属性。

    • 这些属性按相同的顺序声明,具有相同的名称和相同的推断类型。 名称比较不区分大小写。

    • 至少有一个属性是键属性,关键字 Key 应用于相同的属性。

    • 比较每对相应的键属性将返回 True

      例如,在以下示例中,Equals 仅在 Trueemployee01 中返回 employee08。 每行前的注释指定新实例不匹配 employee01的原因。

      Dim employee01 = New With {Key .Name = "Bob", Key .Category = 3, .InOffice = False}
      
      ' employee02 has no InOffice property.
      Dim employee02 = New With {Key .Name = "Bob", Key .Category = 3}
      
      ' The first property has a different name.
      Dim employee03 = New With {Key .FirstName = "Bob", Key .Category = 3, .InOffice = False}
      
      ' Property Category has a different value.
      Dim employee04 = New With {Key .Name = "Bob", Key .Category = 2, .InOffice = False}
      
      ' Property Category has a different type.
      Dim employee05 = New With {Key .Name = "Bob", Key .Category = 3.2, .InOffice = False}
      
      ' The properties are declared in a different order.
      Dim employee06 = New With {Key .Category = 3, Key .Name = "Bob", .InOffice = False}
      
      ' Property Category is not a key property.
      Dim employee07 = New With {Key .Name = "Bob", .Category = 3, .InOffice = False}
      
      ' employee01 and employee 08 meet all conditions for equality. Note 
      ' that the values of the non-key field need not be the same.
      Dim employee08 = New With {Key .Name = "Bob", Key .Category = 2 + 1, .InOffice = True}
      
      ' Equals returns True only for employee01 and employee08.
      Console.WriteLine(employee01.Equals(employee08))
      
  • GetHashcode 提供一个合适的且独特的 GetHashCode 算法。 该算法仅使用键属性来计算哈希代码。

  • ToString 返回串联属性值的字符串,如以下示例所示。 包括键属性和非键属性。

    Console.WriteLine(employee01.ToString())
    Console.WriteLine(employee01)
    ' The preceding statements both display the following:
    ' { Name = Bob, Category = 3, InOffice = False }
    

匿名类型的显式命名属性不能与这些生成的方法冲突。 也就是说,不能使用.Equals.GetHashCode.ToString命名属性。

包含至少一个键属性的匿名类型定义也实现 System.IEquatable<T> 接口,其中 T 是匿名类型的类型。

注释

仅当它们在同一程序集中发生时,匿名类型声明才会创建相同的匿名类型,它们的属性具有相同的名称和相同的推断类型,属性按相同的顺序声明,并且相同的属性被标记为键属性。

另请参阅