如何在自定义类中返回Property的值

gsp*_*ech 1 vb.net

我有一个名为Location的自定义vb,net类,具有多个属性.

类=位置

财产=街道

价值="主要街道"

财产=城市

值="AnyTown"

财产=国家

价值="美国"

通过反思,我可以得到每个属性的名称:

Public Function GetLocationValue(ByRef sLocation)    
Dim sTable As New ProjectSchema.Location
sTable = sLocation
For Each p As System.Reflection.PropertyInfo In sTable.GetType().GetProperties()
        If p.CanRead Then
           Console.WriteLine(p.Name)
        End If
Next
End Function
Run Code Online (Sandbox Code Playgroud)

结果:

p.Name =街

p.Name =城市

p.Name =国家

如何获取每个p.Name的值并返回"Main St","AnyTown"o"USA"

Ree*_*sey 5

您只需从属性信息中获取值:

Dim val as Object
For Each p As System.Reflection.PropertyInfo In sTable.GetType().GetProperties()
    If p.CanRead Then
       Console.WriteLine(p.Name)
       val = p.GetValue(sTable, Nothing)
       Console.WriteLine(val)
    End If
Next
Run Code Online (Sandbox Code Playgroud)