Click here to Skip to main content
13,552,183 members
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 1 Mar 2007

A generic data reader with ILGenerator

, 1 Mar 2007
Rate this:
Please Sign up or sign in to vote.
Generating a delegate at runtime that reads from a DataReader. By reflection, it’s possible to bind directly a simple query to a list of objects.


When doing a data access layer, have you ever wondered if it was be possible to receive a list of objects instead of a DataReader. This is possible with refection and code generation. This saves a lot of time and if you think that this method is slower that the traditional one, it's false. The performance is somewhat equal to the traditional method.


The last time I worked on a data access layer, I found myself repeating the same lines of code over and over. I executed a DataReader and read each column in a while statement. This method leads to many runtime errors. We have to remember the index of each column. What if we add a column at the beginning without changing the indexes? So I developed a method that binds a class to a query. By analysing the class though reflection, we are able to auto generate the code that uses the data reader.

I have made a little project to test the performance of the GenericReader. To do so, I implemented a IDataReader that is working only in memory in order to have the most reliable benchmarks.

Using the code

In order to do this, we can use the reflection. The idea is to bind a field to a column of a select statement.

As an example, I declare a class with 4 fields. Each field has an attribute with a column name. We can see that the fields are read only; this is possible with the code IL. Furthermore, we can see that I use a double? in order to tell that a value type can be null. The verification is made with the DBNull and the program sets the value to null. This is easier to use.

public struct TestClass
    public readonly long Field1;
    public readonly string Field2;
    public readonly double? Field3;
    public readonly bool Field4;

The last thing we have to do is to use the GenericReader. The following method lets us see how easy it is.

GenericReader genericReader = new GenericReader();
private List<testclass /> ReaderExample(SqlConnection connection)
   SqlCommand command = new SqlCommand("SELECT Field1, Field2, Field3, Field4 " +
                                       "FROM AnyTable");
   return genericReader.ReadReader<testclass />(command.ExecuteReader());

The GenericReader is using the codeIL to generate a delegate the first time we use a Type. This delegate is use over an over to read the DataReader.

Points of Interest

The most useful thing is that the code is somewhat easier to maintain. Especially when we need to add an information in a select statement. In this case, it's very easy to make mistakes with the indexed of the DataReader.


At first, I use reflection to read the data instead of creating a delegate with codeIL. The need came quickly to have a faster way. So I change it a bit. CodeIL is a lot harder to implement, but it was worth it. The people that used the GenericReader told me that they saved a lot of time coding there data access layer.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
BeyMelamed7-Jan-13 18:15
memberBeyMelamed7-Jan-13 18:15 
QuestionProperties instead of fields Pin
hhilzinger28-Sep-11 1:11
memberhhilzinger28-Sep-11 1:11 
GeneralA generic data reader with ILGenerator Pin
Member 412624020-Oct-09 3:30
memberMember 412624020-Oct-09 3:30 
GeneralI love it !!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
ewds28-Feb-09 19:31
memberewds28-Feb-09 19:31 
Questiontype.GetFields() bug? Pin
psykoptic4-Mar-08 13:17
memberpsykoptic4-Mar-08 13:17 
Generalvery cool - but not so well advertised Pin
christoph braendle2-Mar-07 1:21
memberchristoph braendle2-Mar-07 1:21 
GeneralRe: very cool - but not so well advertised Pin
rashack30-Apr-07 9:09
memberrashack30-Apr-07 9:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180515.1 | Last Updated 1 Mar 2007
Article Copyright 2007 by Henri G Demers
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid