我需要检索实例对象的名称(而不是类型名称...)
我已经看到GetProperties()函数获取子属性名称,但是我需要当前对象的名称
Public Class Class1
Private mValore As String
Public Property Valore As String
Get
Return mValore
End Get
Set(value As String)
mValore = value
End Set
End Property
End Class
Public Class Class2
Private mMickey As new Class1
Public Property Mickey As Class1
Get
Return mMickey
End Get
Set(value As Class1)
mMickey = value
End Set
End Property
End Class
我需要在Class1中获取Class2中实例对象的名称:“ Mickey”
可能吗 ?感谢您提出的所有建议。
正如Hans Passant提到的,对象没有名称。
因此,如果您确实需要名称,则可以将它们作为属性或字段进行介绍。您可以使用CallerMemberNameAttribute自动将呼叫者名称传递给例如构造函数。
另一件事是,对象可能是在外部创建的Class2
,实际上是在Mickey ... Set
您mMickey
要从外部某处将字段分配给对象的设置器中创建的,因此该对象可能具有不同的名称。我宁愿创建对象的副本,而不仅仅是分配,然后我们可以为其分配任何名称,并且该名称不会与先前的名称冲突。例如:
Imports System.Runtime.CompilerServices
Public Class Class1
Private mValore As String
Public ReadOnly Name As String
Public Sub New(mValore As String, <CallerMemberName> Optional callerMemberName As String = Nothing)
Me.mValore = mValore
Me.Name = callerMemberName
End Sub
Public ReadOnly Property Valore As String
Get
Return mValore
End Get
End Property
End Class
Public Class Class2
Private mMickey As Class1
Public Property Mickey As Class1
Get
Return mMickey
End Get
Set(value As Class1)
mMickey = New Class1(mValore:=value.Valore)
End Set
End Property
End Class
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句