Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8483415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:05:50+00:00 2026-06-10T20:05:50+00:00

This is a serializer I’ve been working on that serializes datarow’s into objects based

  • 0

This is a serializer I’ve been working on that serializes datarow’s into objects based on some mapped property attributes. I read a number of posts related to using [Delegate].CreateDelegate to increase speed – so I gave that a try. It seems to have gained some performance increase, but minimal at that (even after an initial looping of all object types to cache all possible property info objects and such). The mapped property info’s just have an alternate DB column name that can be assigned, because we don’t have complete control over all column names I use. It also only grabs properties I want to map.

Right now, the serialization of approx. 1500 objects with an average of 13 properties per object, and an average of 3 object within each of the 1500 parent object (as children) also being populated with an average of 13 properties a piece – boast these times for my comparison:

manually setting a property.Name = row.Item(“columnName”)
785 ms

p.SetValue
1490 ms

GetSetMethod.Invoke
1585 ms

[Delegate].CreateDelegate
1285 ms

Can anyone suggest something more I can do to increase performance??

  Friend Class DataSerializer(Of T As Class)

        Private Shared MappedPropertiesCache As New Dictionary(Of Type, PropertyInfo())
        Private Shared MappedColumnNameCache As New Dictionary(Of Type, List(Of String))

        Private Shared ActionCache As New Dictionary(Of String, Action(Of T, Object))

        Friend Shared Function SerializeDataRow(ByVal row As DataRow) As T

            Dim target As Object = Activator.CreateInstance(GetType(T))

            Dim targetType As Type = GetType(T)

            AnalyzeMappedCache(target, targetType)

            Dim index As Integer = 0

            Dim mappedColumns As List(Of String) = MappedColumnNameCache.Item(targetType)

            'iterate through target object properties
            For Each p As PropertyInfo In MappedPropertiesCache.Item(targetType)

                If row.Table.Columns.Contains(mappedColumns.Item(index)) Then


                    '' SLOW
                    'p.SetValue(target, CheckDbNull(row.Item(mappedColumns.Item(index))), Nothing)


                    '' SLOWER
                    'Dim methodInfo As MethodInfo = p.GetSetMethod()

                    'methodInfo.Invoke(target, New Object() {CheckDbNull(row.Item(mappedColumns.Item(index)))})



                    ''FASTER THAN PREVIOUS TWO, BUT STILL SLOW


                    'Dim key As String = String.Format("{0}:{1}", target.GetType.FullName, p.Name)

                    'If Not ActionCache.ContainsKey(key) Then

                    '    Dim methodAction As Action(Of T, Object) = MagicMethod(p.GetSetMethod())

                    '    ActionCache.Add(key, methodAction)

                    'End If

                    'Dim param As Object = CheckDbNull(row.Item(mappedColumns.Item(index)))

                    'If Not param Is Nothing Then

                    '    ActionCache(key)(target, param)

                    'End If

                End If

                index = index + 1

            Next

            Return target

        End Function


        Private Shared Function MagicMethod(method As MethodInfo) As Action(Of T, Object)
            ' First fetch the generic form
            Dim genericHelper As MethodInfo = GetType(DataSerializer(Of T)).GetMethod("MagicMethodHelper", BindingFlags.[Static] Or BindingFlags.NonPublic)

            ' Now supply the type arguments
            Dim constructedHelper As MethodInfo = genericHelper.MakeGenericMethod(GetType(T), method.GetParameters()(0).ParameterType)

            ' Now call it. The null argument is because it's a static method.
            Dim ret As Object = constructedHelper.Invoke(Nothing, New Object() {method})

            ' Cast the result to the right kind of delegate and return it
            Return DirectCast(ret, Action(Of T, Object))
        End Function

        Private Shared Function MagicMethodHelper(Of TTarget As Class, TParam)(method As MethodInfo) As Action(Of TTarget, Object)
            ' Convert the slow MethodInfo into a fast, strongly typed, open delegate
            Dim func As Action(Of TTarget, TParam) = DirectCast([Delegate].CreateDelegate(GetType(Action(Of TTarget, TParam)), method), Action(Of TTarget, TParam))

            ' Now create a more weakly typed delegate which will call the strongly typed one
            Dim ret As Action(Of TTarget, Object) = Sub(target As TTarget, param As Object) func(target, CType(param, TParam))
            Return ret
        End Function

        Private Shared Sub AnalyzeMappedCache(ByVal target As Object, ByVal targetType As Type)

            'this assumes the target object inherits from BaseProperties
            If Not MappedPropertiesCache.ContainsKey(targetType) Then

                Dim props As PropertyInfo() = target.GetMappedProperties()

                Dim mappedColumnNameList As New List(Of String)

                For Each prop As PropertyInfo In props

                    mappedColumnNameList.Add(CType(prop.GetCustomAttributes(GetType(DTO_POMGMT.MappedProperty), True).FirstOrDefault, DTO_POMGMT.MappedProperty).ColumnName)

                Next

                MappedColumnNameCache.Add(targetType, mappedColumnNameList)

                MappedPropertiesCache.Add(targetType, props)

            End If

        End Sub

        'check for a dbnull value of any object type returned from database
        Private Shared Function CheckDbNull(ByVal obj As Object) As Object

            Return If(obj Is DBNull.Value, Nothing, obj)

        End Function

    End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:05:51+00:00Added an answer on June 10, 2026 at 8:05 pm

    I would suggest that you take a look at FastMember, either directly, or just borrow and adapt the code. In particular (switching a little to C#):

    var accessor = TypeAccessor.Create(targetType); 
    foreach(PropertyInfo p in MappedPropertiesCache.Item(targetType))
    {
        ...
        accessor[target, p.Name] = ... // newValue
        ...
    }
    

    Alternatively, take a look at how something like dapper-dot-net handles member-wise assignment.

    As a side note: a static dictionary is not thread-safe, and you might want to be careful accessing that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some code that looks like this: class Serializer { public: template<class Type>
JavaScriptSerializer serializer = new JavaScriptSerializer(); string sJSON = serializer.Serialize(pt); This works fine except that
For some reason, this code won't compile: JsonSerializer serializer = new JsonSerializer(); _sectionStories =
I'm sure this question has been asked over and over again, but for some
I have some code like this: JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.RegisterConverters(new JavaScriptConverter[] {
While using this code to serialize an object: public object Clone() { var serializer
using (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();
I'm running a desktop application and when I reach this line: serializer.Serialize(new StringWriter(sb), value);
this dataserializer is great for performance. but I keep getting stuck on datacolumns that
I know this is a vague open ended question. I'm hoping to get some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.