VB.Net按名称获取属性值

时间:2015-04-13 19:42:47

标签: sql vb.net datatable

我正在尝试创建一个构造函数,将数据从数据库加载到Properties中。 我已经有了工作函数来提取表数据和表列名。 我试图弄清楚如何获取和设置属性的数据。 一切正常,直到我到达getValue和setValue函数。 我做错了什么?

谢谢,

Sub New(ID As Integer)
        Dim strTable As String = "Ration"
        Dim strField As String = "RationID"
        Dim queryString As String = "Select * From " & strTable & " Where " & strField & " = '" & ID & "';"
        Console.WriteLine("SQL: " & queryString)
        Dim dtDataTable As DataTable = DataManager.RunQueryForDataTable(queryString)
        Dim dtColumns As DataTable = DataManager.getColumnNames(strTable)
        For Each row As DataRow In dtDataTable.Rows
            For Each col As DataRow In dtColumns.Rows
                Dim pinfo As PropertyInfo = Me.GetType().GetProperty(col("Column Name"))
                Console.WriteLine("Column: " & col("Column Name").ToString)
                Console.WriteLine("Name: " & pinfo.Name)
                Console.WriteLine("Type: " & pinfo.PropertyType.ToString)
                Console.WriteLine("Value: " & pinfo.GetValue(Me, New Object() {0}))
            Next
        Next
    End Sub

1 个答案:

答案 0 :(得分:1)

发现问题

Console.WriteLine("Value: " & pinfo.GetValue(Me, New Object() {0}))

需要

Console.WriteLine("Value: " & pinfo.GetValue(Me, Nothing))