Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Generic List Provider in CSharp

From Wiki

Jump to: navigation, search

This is a community-editable place holder for the code discussed here

Have at it!

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Reflection;
  5. using System.Data;
  6.  
  7. namespace MyApp.Utilities
  8. {
  9.     public class ListProvider<T> where T: new()
  10.     {
  11.         public List<T> FindAll(IDbCommand com, IDbConnection con)
  12.         {
  13.             //ensure that command object's connection is set, open connection
  14.             com.Connection = con;
  15.             con.Open();
  16.  
  17.             //create data reader used in filling objects
  18.             IDataReader rdr = com.ExecuteReader();
  19.  
  20.             //instantiate new list of <T> that will be returned
  21.             List<T> returnList = new List<T>();
  22.  
  23.             //need a Type and PropertyInfo object to set properties via reflection
  24.             Type tType = new T().GetType();
  25.             PropertyInfo pInfo;
  26.  
  27.             //x will hold the instance of <T> until it is added to the list
  28.             T x;
  29.  
  30.             //use reader to populate list of objects
  31.             while (rdr.Read())
  32.             {
  33.                 x = new T();
  34.  
  35.                 //set property values
  36.                 //for this to work, command's column names must match property names in object <T>
  37.                 for (int i = 0; i<rdr.FieldCount; i++)
  38.                 {
  39.                     pInfo = tType.GetProperty(rdr.GetName(i));
  40.                     pInfo.SetValue(x, rdr[i], null);
  41.                 }
  42.  
  43.                 //once instance of <T> is populated, add to list
  44.                 returnList.Add(x);
  45.             }
  46.  
  47.  
  48.             //clean up -- assumes you don't need command anymore
  49.             con.Close();
  50.             com.Dispose();
  51.             rdr.Dispose();
  52.  
  53.             return returnList;
  54.         }
  55.     }
  56. }

Feel free to add to this page, or make edits to the code (just try to provide a brief explanation of the edit, so everyone gets a little smarter).

323 Rating: 3.1/5 (20 votes cast)