具有属性构造函数的命名参数
Named parameters with attribute constructors
阅读有关使用属性的文档:https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/attributes/,它说允许使用命名参数。我读到它作为构造函数的命名参数,但这似乎不起作用:
Public Class FullName
Inherits System.Attribute
Public Property Name As String
Public Property Hey As String
Sub New(FirstName As String, LastName As String)
Name = FirstName + " " + LastName
End Sub
End Class
<FullName(LastName:="moreno", FirstName:="John", Hey:="joe")>
Public Class Example
Public Sub Test
Dim x = New FullName(LastName:="moreno", FirstName:="john")
End Sub
End Class
属性是否不支持 vb 中构造函数的命名参数,或者我只是缺少正确的语法?
实际的构造函数参数没有命名。命名参数用于设置属性。您可以将其视为非常像常规代码中的对象初始化器:
Dim fn As New FullName("John", "moreno") With {.Hey = "joe"}
<FullName("John", "moreno", Hey:="joe")>
构造函数参数是该文档中引用的位置参数,然后命名参数是您可以设置也可以不设置的属性。
阅读有关使用属性的文档:https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/attributes/,它说允许使用命名参数。我读到它作为构造函数的命名参数,但这似乎不起作用:
Public Class FullName
Inherits System.Attribute
Public Property Name As String
Public Property Hey As String
Sub New(FirstName As String, LastName As String)
Name = FirstName + " " + LastName
End Sub
End Class
<FullName(LastName:="moreno", FirstName:="John", Hey:="joe")>
Public Class Example
Public Sub Test
Dim x = New FullName(LastName:="moreno", FirstName:="john")
End Sub
End Class
属性是否不支持 vb 中构造函数的命名参数,或者我只是缺少正确的语法?
实际的构造函数参数没有命名。命名参数用于设置属性。您可以将其视为非常像常规代码中的对象初始化器:
Dim fn As New FullName("John", "moreno") With {.Hey = "joe"}
<FullName("John", "moreno", Hey:="joe")>
构造函数参数是该文档中引用的位置参数,然后命名参数是您可以设置也可以不设置的属性。