Tesing .NET reflection

This is an interesting piece of code, it took me a day to write it and I don't want to lose it so I'm going to blog it here.

The code uses reflection to populate an eConnect class (taSOPHdrIvcInsert... but it'll work for any class) with data from a stored procedure... by iterating through the class.

Reflection is cool.

Related Articles

... and you 'll find more on the NET Development Menu

 

Private Sub ReflectionTest()
    Try
        'get an instance of an eConnect class
        Dim otaSopHdrIvcInsert As New Microsoft.Dynamics.GP.eConnect.Serialization.taSopHdrIvcInsert
 
        'get a datatable that has data from live dynamics data, we'll get all the fields from a SOP10100 order
        Dim oDT As DataTable = SPs.FP_SOP10100_SEL_byID(2, "STD00582719", App.Database).getTable
        'this data table will only ever have one row, get a reference to the first row.
        Dim oRow As DataRow = oDT.Rows(0)
 
        'using Reflection, get a 'FieldInfo' object for the taSopHdrIvcInsert.SOPNUMBE field
        Dim oFieldInfo As FieldInfo = otaSopHdrIvcInsert.GetType().GetField("SOPNUMBE")
 
        'using Reflection, populate that field with the data from the data table.
        oFieldInfo.SetValue(otaSopHdrIvcInsert, Convert.ChangeType(oRow(oFieldInfo.Name), oFieldInfo.FieldType))
 
        'prove that it worked
        Dim s As String = otaSopHdrIvcInsert.SOPNUMBE
    Catch ex As Exception
        ErrorHandler.globalErrorHandler(ex, True)
    End Try
 
End Sub

 

 Same thing, but this time loop through the entire taSopHdrIvcInsert object and populate it from the stored procedure.

I love my job.  

 

Private Sub ReflectionTest()
    Try
        'get an instance of an eConnect class
        Dim otaSopHdrIvcInsert As New Microsoft.Dynamics.GP.eConnect.Serialization.taSopHdrIvcInsert
 
        'get a datatable that has data from live dynamics data, we'll get all the fields from a SOP10100 order
        Dim oDT As DataTable = SPs.FP_SOP10100_SEL_byID(2, "STD00582719", App.Database).getTable
        'this data table will only ever have one row, get a reference to the first row.
        Dim oRow As DataRow = oDT.Rows(0)
 
        'using Reflection, get a 'FieldInfo' object for the taSopHdrIvcInsert.SOPNUMBE field
        'this will loop through the entire taSopHdrIvcInsert object and populate all the fields
        'cool, right?
        For Each oFieldInfo As FieldInfo In otaSopHdrIvcInsert.GetType().GetFields
 
            'using Reflection, populate that field with the data from the data table.
            oFieldInfo.SetValue(otaSopHdrIvcInsert, Convert.ChangeType(oRow(oFieldInfo.Name), oFieldInfo.FieldType))
        Next
 
 
        'prove that it worked
        Dim s As String = otaSopHdrIvcInsert.SOPNUMBE
    Catch ex As Exception
        ErrorHandler.globalErrorHandler(ex, True)
    End Try
 
End Sub

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences