Click here to Skip to main content
15,881,559 members
Articles / Web Development / ASP.NET

Dynamic... But Fast: The Tale of Three Monkeys, A Wolf and the DynamicMethod and ILGenerator Classes

Rate me:
Please Sign up or sign in to vote.
4.94/5 (272 votes)
12 Jun 2012BSD6 min read 1M   1.2K   384  
How to use the DynamicMethod and ILGenerator classes to create dynamic code at runtime that outperforms Reflection.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Reflection;
using System.Reflection.Emit;

namespace DynamicMappingSpike
{
    public class Program
    {

        static void Main(string[] args)
        {
            SqlConnection connection = new SqlConnection("Data Source=localhost; Initial Catalog=SpikeDB;Integrated Security=True");
            SqlCommand command = new SqlCommand("Select * From LoadSpike2", connection);
            connection.Open();

            LoadManually(command);
            LoadUsingReflection(command);
            LoadUsingDynamicCode(command);

            connection.Close();
            Console.WriteLine("Done!");
            Console.Read();
        }

        private static void LoadManually(SqlCommand command)
        {
            DateTime start = DateTime.Now;
            Console.WriteLine("Start Manual Load: {0}", start);

            using (SqlDataReader reader = command.ExecuteReader())
            {
                ManualBuilder builder = new ManualBuilder();
                while (reader.Read())
                {
                    Person person = builder.Build(reader);
                }
            }

            DateTime stop = DateTime.Now;
            Console.WriteLine("Stop Manual Load: {0}", stop);
            Console.WriteLine("Elapsed Time: {0}", stop - start);
            Console.WriteLine();
        }

        private static void LoadUsingReflection(SqlCommand command)
        {
            DateTime start = DateTime.Now;
            Console.WriteLine("Start Reflection Load: {0}", start);

            using (SqlDataReader reader = command.ExecuteReader())
            {
                ReflectionBuilder<Person> builder = ReflectionBuilder<Person>.CreateBuilder(reader);
                while (reader.Read())
                {
                    Person person = builder.Build(reader);
                }
            }

            DateTime stop = DateTime.Now;
            Console.WriteLine("Stop Reflection Load: {0}", stop);
            Console.WriteLine("Elapsed Time: {0}", stop - start);
            Console.WriteLine();
        }

        private static void LoadUsingDynamicCode(SqlCommand command)
        {
            DateTime start = DateTime.Now;
            Console.WriteLine("Start Dynamic Load: {0}", start);

            using (SqlDataReader reader = command.ExecuteReader())
            {
                DynamicBuilder<Person> builder = DynamicBuilder<Person>.CreateBuilder(reader);                
                while (reader.Read())
                {
                    Person person = builder.Build(reader);
                }
            }

            DateTime stop = DateTime.Now;
            Console.WriteLine("Stop Dynamic Load: {0}", stop);
            Console.WriteLine("Elapsed Time: {0}", stop - start);
            Console.WriteLine();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer (Senior) Scratch Audio
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions