Thursday, September 15, 2011

Imports Solution.DataExtensions

Imports System.Runtime.CompilerServices

Module DataExtensions

    
    Public Sub Deserialize(ByVal this As Object, ByVal row As Dictionary(Of String, Object))
        For Each Field As KeyValuePair(Of String, Object) In row
            Dim intVal As System.Object() = {Field.Value}
            this.GetType().InvokeMember(
                Field.Key,
                System.Reflection.BindingFlags.SetProperty,
                Type.DefaultBinder,
                this,
                intVal
                )
        Next
    End Sub

    
    Public Function ToDataRow(ByVal this As Object) As Dictionary(Of String, Object)
        Return Enumerable.Range(0, this.FieldCount).ToDictionary(
            Function(field) this.GetName(field).ToString(),
            Function(field) this.GetValue(field)
                )
    End Function

End Module

0 comments: