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