匿名类型定义 (Visual Basic)

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

编译器生成的代码

对于 product 的以下定义,编译器将创建一个包含属性 NamePriceOnHand 的新的类定义。

' 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 仅为 employee01employee08 返回 True。 每行之前的注释指定新实例不匹配 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 是匿名类型的类型。

注意

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

另请参阅