我有一个名为Location的自定义vb,net类,具有多个属性。
实施例
Class = Location
Property = Street
Value =“Main St”
财产=城市
Value =“AnyTown”
Property = Country
价值=“美国”
通过反思,我可以得到每个属性的名称:
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
结果:
p.Name = Street
p.Name =城市
p.Name =国家
如何获取每个p.Name的值并返回“Main St”,“AnyTown”o“USA”
答案 0 :(得分: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
答案 1 :(得分:2)
其中p是您的PropertyInfo对象。
p.GetValue (sTable, Nothing)