所以这是我的班级结构的一部分:
Public Class CParticle
Public Feature As Double
Public AreaName As String
......
Public ElementsWT As SElements 'Elements in wt%
Public ElementsAT As SElements 'Elements in at%
Public Sub New()
ElementsWT = New SElements
ElementsAT = New SElements
End Sub
End Class
使用这个'子类':
Public Class SElements
Public B As Double
Public C As Double
Public N As Double
Public O As Double
Public F As Double
....
End Class
现在我想要访问CParticle实例中的所有变量(例如称为'Particle')以及它们的名称(String)的SElements实例。
例如:“Feature”应该允许我访问Particle.Feature
目前我正在使用反思:
...
Dim myFieldInfo As FieldInfo
myFieldInfo = GetType(CParticle).GetField("Feature")
If Not myFieldInfo Is Nothing Then myFieldInfo.SetValue(Particle, value)
这很有效。但我如何访问,例如Particle.ElementsWT.B,字符串为“ElementsWT.B”?除了使用反射之外,还有更好的方法吗?