Vb.Net 通过反射获取类的字段值

前端之家收集整理的这篇文章主要介绍了Vb.Net 通过反射获取类的字段值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

'IU2DefineData类名

'IU2DefineData类中IU2AddressInf类名

'address是IU2AddressInf类中的字段

Private Shared mIU2Data As IU2DefineData

Public Function GetDate() As DataTable
Dim t As Type = GetType(IU2DefineData)
Dim address As FieldInfo = GetType(IU2AddressInf).GetField("address`")
Dim value As FieldInfo = GetType(IU2AddressInf).GetField("value")
Dim comment As FieldInfo = GetType(IU2AddressInf).GetField("Comment")
Dim dt As DataTable = New DataTable
Dim rows As DataRow
Dim fieldKey As String = Nothing
Dim fields() As FieldInfo
Try
dt.Columns.Add("address",GetType(String))
dt.Columns.Add("value",GetType(String))
dt.Columns.Add("`data",GetType(String))
dt.Columns.Add("Comment",GetType(String))
fields = t.GetFields()
For Each field As FieldInfo In fields

'取到类中变量值 fieldKey = field.Name Dim addressValue As String = address.GetValue(field.GetValue(mIU2Data)) Dim strvalue As String = value.GetValue(field.GetValue(mIU2Data)) Dim strComment As String = comment.GetValue(field.GetValue(mIU2Data)) rows = dt.NewRow rows.Item("address") = fieldKey rows.Item("value") = addressValue rows.Item("data") = strvalue rows.Item("Comment") = strComment dt.Rows.Add(rows)` Next Catch ex As Exception End Try Return dt End Function

原文链接:https://www.f2er.com/vb/259997.html

猜你在找的VB相关文章