问题描述
我有一个带有以下 Property
的 vb Class
:
I have a vb Class
with the following Property
in it:
公共财产总线为整数
这是否等同于更详细的属性?
Is this equivalent to a more detailed property?
编译器是否真的在后台将这行代码转换为更详细的Property
结构,其中包含一个字段_Buses
?
Does the compiler actually transform, in the background, this line of code into a more detailed Property
structure which includes a field _Buses
?
因此,只要我使用结构 Public Property x AS y
,无需实际声明 _Buses
,那么这些字段之一将可用吗?
Therefore without actually declaring _Buses
aslong as I use the structure Public Property x AS y
then one of these fields will be available?
编辑
实际上不太确定是否可以添加比 此处在 MSDN 上
推荐答案
Short Answers
问:编译器是否真的在后台将这行代码转换为包含字段_Buses
的更详细的属性结构?
答:是
Short Answers
Q: Does the compiler actually transform, in the background, this line of code into a more detailed Property structure which includes a field _Buses
?
A: Yes
问:无需实际声明 _Buses
只要我使用结构 Public Property x AS y 那么这些字段之一将可用吗?
答:是
Q: Without actually declaring _Buses
aslong as I use the structure Public Property x AS y then one of these fields will be available?
A: Yes
自动实现的属性通常是您没有为属性的 Get
和 Set
部分明确指定代码的属性.自动实现属性的一般定义如下:
Auto-implemented properties are generally properties where you do not explicitly specify code for the Get
and Set
parts of the property. The general definition of an auto-implemented property is as follows:
Public Property Age As Integer
或
Public Property Age As Integer = 5
在这两种情况下,编译器都会自动为您生成所有支持字段和初始值设定项.
In both cases, the compiler generates all the backing fields and initializers for you automatically.
考虑以下具有两个自动实现的属性(Name
和 Age
)和一个常规属性(Address
)的类.
Consider the following class with two auto-implemented properties (Name
and Age
) and one regular property (Address
).
Public Class Person
Dim _address As String
Public Sub New()
_address = "4, Hutchinson Road"
End Sub
Public Property Name As String
Public Property Age As Integer = 3
Public Property Address As String
Get
Return _address
End Get
Set(value As String)
_address = value
End Set
End Property
Public Overrides Function ToString() As String
Return _Name & " Age: " & Me.Age.ToString()
End Function
End Class
编译器自动为 Name
和 Age
属性生成支持字段以及 Get
和 Set
方法.生成的字段与前面带有下划线的属性同名.因此,Name
属性的支持字段是 _Name
,而 Age
属性的支持字段是 _Age
.
The compiler automatically generates backing fields as well as Get
and Set
methods for the Name
and Age
properties.The fields generated have the same name as the property with a preceding underscore. Therefore the Name
property's backing field is _Name
and the Age
property's backing field is _Age
.
自动生成的字段还附加了 DebuggerBrowsable(DebuggerBrowsableState.Never)
和 CompilerGenerated
属性.
The auto-generated fields also have the DebuggerBrowsable(DebuggerBrowsableState.Never)
and CompilerGenerated
attributes attached to them.
DebuggerBrowsable
属性可防止该字段显示在代码编辑器的自动完成列表中.但是,这并不妨碍您直接在代码中访问该字段,正如您在 ToString
方法中看到的那样,我直接使用了 _Name
字段.CompilerGenerated
属性表明该字段是由编译器创建的.
The DebuggerBrowsable
attributes prevents the field from being displayed in the Auto-Complete list in the code editor. This, however, does not prevent you from accessing the field directly in your code, as you can see in the ToString
method where I use the _Name
field directly.
The CompilerGenerated
attribute indicates that the field was created by the compiler.
Age
属性(以及所有带有初始化器的自动实现的属性)在类的默认构造函数中初始化.
The Age
property (as well as all auto-implemented properties with initializers) is initialized in the class's default constructor.
上述类的反编译版本如下所示:
The decompiled version of the class above looks like this:
Public Class Person
' Methods
Public Sub New()
Me.Age = 3
Me._address = "4, Hutchinson Road"
End Sub
Public Overrides Function ToString() As String
Return String.Join(" ", New String() { Me._Name, Me.Age.ToString })
End Function
' Properties
Public Property Address As String
Get
Return Me._address
End Get
Set(ByVal value As String)
Me._address = value
End Set
End Property
Public Property Age As Integer
<DebuggerNonUserCode> _
Get
Return Me._Age
End Get
<DebuggerNonUserCode> _
Set(ByVal AutoPropertyValue As Integer)
Me._Age = AutoPropertyValue
End Set
End Property
Public Property Name As String
<DebuggerNonUserCode> _
Get
Return Me._Name
End Get
<DebuggerNonUserCode> _
Set(ByVal AutoPropertyValue As String)
Me._Name = AutoPropertyValue
End Set
End Property
' Fields
Private _address As String
<CompilerGenerated, DebuggerBrowsable(DebuggerBrowsableState.Never)> _
Private _Age As Integer
<DebuggerBrowsable(DebuggerBrowsableState.Never), CompilerGenerated> _
Private _Name As String
End Class
如您所见,字段 _Name
和 _Age
是自动生成的,因此您可以在代码中使用它们而不会出现任何问题.
As you can see, the fields _Name
and _Age
are generated for you automatically so you can use them in your code without any problems.
这篇关于什么是自动实现的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!