2 Replies - 284 Views - Last Post: 06 December 2012 - 02:19 PM Rate Topic: -----

#1 dotnetlover  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-December 12

Generic Dictionary Mapper for Critiques

Posted 06 December 2012 - 12:23 PM

Submitting this for review to try to weed out *obvious* or sneaky errors I don't address or even just to see if the whole thing is redundant and already exists in the framework. :)/>

Otherwise, if you need to use it: enjoy.

    Public Class DictionaryMapper(Of T As {Class, New})
        Public Shared Function MapDictionaryToObject(ByVal entity As T, ByVal dictionary As IDictionary) As T
            Dim properties = entity.GetType().GetProperties()

            For Each pInfo As System.Reflection.PropertyInfo In properties
                If dictionary.Contains(pInfo.Name) Then
                    Dim infoType = pInfo.PropertyType
                    Dim nullableType = Nullable.GetUnderlyingType(pInfo.PropertyType)

                    If Not nullableType Is Nothing Then
                        infoType = nullableType
                    End If

                    Dim conversion = Convert.ChangeType(dictionary(pInfo.Name), infoType)

                    pInfo.SetValue(entity, conversion, Nothing)
                End If
            Next

            Return entity
        End Function
    End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Generic Dictionary Mapper for Critiques

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9479
  • View blog
  • Posts: 35,743
  • Joined: 12-June 08

Re: Generic Dictionary Mapper for Critiques

Posted 06 December 2012 - 02:16 PM

Quote

Otherwise, if you need to use it: enjoy.

Just say'n - we have a snippets section you should submit this to if that is your goal.
Was This Post Helpful? 0
  • +
  • -

#3 dotnetlover  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-December 12

Re: Generic Dictionary Mapper for Critiques

Posted 06 December 2012 - 02:19 PM

View Postmodi123_1, on 06 December 2012 - 02:16 PM, said:

Quote

Otherwise, if you need to use it: enjoy.

Just say'n - we have a snippets section you should submit this to if that is your goal.

Perhaps after this sits here a while. The goal really is a review/critique/"hey n00b use System.XXX instead".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1