Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Reflection » Downloads
 
Add your own
alternative version

Introduction to Creating Dynamic Types with Reflection.Emit: Part 2

, 1 May 2006 CPOL
Part 2 of an introduction to creating dynamic types. This article shows how to actually generate the methods in a dynamic type and how to call them.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using DotNetPerformance;
using DotNetPerformance.ResultOutput;

namespace Johns_Perf_Testing
{
	/// <summary>
	/// Summary description for DataSetOrdinalTest.
	/// </summary>
	public class DataSetOrdinalTest
	{
		public TestResultGrp OrdinalTest()
		{
			const int iterations = 10000;
			const int runs = 5;

			TestRunner tr = new TestRunner(iterations, runs);
			
			TestRunner.TestCase testCases = null;

			testCases += new TestRunner.TestCase(this.IntOrdinal);
			testCases += new TestRunner.TestCase(this.StringOrdinal);
			testCases += new TestRunner.TestCase(this.SmartOrdinal);
			testCases += new TestRunner.TestCase(this.SmartOrdinal2);

			return tr.RunTests(testCases);
		}

		public TestResultGrp GetStringTest()
		{
			const int iterations = 10000;
			const int runs = 5;

			TestRunner tr = new TestRunner(iterations, runs);
			
			TestRunner.TestCase testCases = null;

			testCases += new TestRunner.TestCase(this.ToStringTest);
			testCases += new TestRunner.TestCase(this.StringCastTest);			

			return tr.RunTests(testCases);
		}

		private DataSet data;

		public DataSetOrdinalTest()
		{
			SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=PerfTesting;Data Source=.");
			con.Open();
			SqlDataAdapter adp = new SqlDataAdapter("select * from TestTable", con);
			data = new DataSet();
			adp.Fill(data);
			con.Close();
		}	



		private void ToStringTest(int iterations)
		{
			for (int i = 0; i < iterations; i++)
			{
				foreach (DataRow row in data.Tables[0].Rows)
				{				
					string temp = string.Empty;
					temp = row[0].ToString();
					temp = row[1].ToString();
					temp = row[2].ToString();
					temp = row[3].ToString();
					temp = row[4].ToString();
					temp = row[5].ToString();
					temp = row[6].ToString();
					temp = row[7].ToString();
					temp = row[8].ToString();
					temp = row[9].ToString();
					temp = row[10].ToString();
					temp = row[11].ToString();
					temp = row[12].ToString();
					temp = row[13].ToString();
					temp = row[14].ToString();
					temp = row[15].ToString();
					temp = row[16].ToString();
					temp = row[17].ToString();
					temp = row[18].ToString();
					temp = row[19].ToString();
					temp = row[20].ToString();
					temp = row[21].ToString();
					temp = row[22].ToString();
					temp = row[23].ToString();
					temp = row[24].ToString();
					temp = row[25].ToString();					
				}
			}	
		}

		private void StringCastTest(int iterations)
		{
			for (int i = 0; i < iterations; i++)
			{
				foreach (DataRow row in data.Tables[0].Rows)
				{				
					string temp = string.Empty;
					temp = row[0] == DBNull.Value ? "" : (string)row[0];
					temp = row[1] == DBNull.Value ? "" : (string)row[1];
					temp = row[2] == DBNull.Value ? "" : (string)row[2];
					temp = row[3] == DBNull.Value ? "" : (string)row[3];
					temp = row[4] == DBNull.Value ? "" : (string)row[4];
					temp = row[5] == DBNull.Value ? "" : (string)row[5];
					temp = row[6] == DBNull.Value ? "" : (string)row[6];
					temp = row[7] == DBNull.Value ? "" : (string)row[7];
					temp = row[8] == DBNull.Value ? "" : (string)row[8];
					temp = row[9] == DBNull.Value ? "" : (string)row[9];
					temp = row[10] == DBNull.Value ? "" : (string)row[10];
					temp = row[11] == DBNull.Value ? "" : (string)row[11];
					temp = row[12] == DBNull.Value ? "" : (string)row[12];
					temp = row[13] == DBNull.Value ? "" : (string)row[13];
					temp = row[14] == DBNull.Value ? "" : (string)row[14];
					temp = row[15] == DBNull.Value ? "" : (string)row[15];
					temp = row[16] == DBNull.Value ? "" : (string)row[16];
					temp = row[17] == DBNull.Value ? "" : (string)row[17];
					temp = row[18] == DBNull.Value ? "" : (string)row[18];
					temp = row[19] == DBNull.Value ? "" : (string)row[19];
					temp = row[20] == DBNull.Value ? "" : (string)row[20];
					temp = row[21] == DBNull.Value ? "" : (string)row[21];
					temp = row[22] == DBNull.Value ? "" : (string)row[22];
					temp = row[23] == DBNull.Value ? "" : (string)row[23];
					temp = row[24] == DBNull.Value ? "" : (string)row[24];
					temp = row[25] == DBNull.Value ? "" : (string)row[25];					
				}
			}	
		}

		private void IntOrdinal(int iterations)
		{
			for (int i = 0; i < iterations; i++)
			{
				foreach (DataRow row in data.Tables[0].Rows)
				{				
					string temp = string.Empty;
					temp = row[0].ToString();
					temp = row[1].ToString();
					temp = row[2].ToString();
					temp = row[3].ToString();
					temp = row[4].ToString();
					temp = row[5].ToString();
					temp = row[6].ToString();
					temp = row[7].ToString();
					temp = row[8].ToString();
					temp = row[9].ToString();
					temp = row[10].ToString();
					temp = row[11].ToString();
					temp = row[12].ToString();
					temp = row[13].ToString();
					temp = row[14].ToString();
					temp = row[15].ToString();
					temp = row[16].ToString();
					temp = row[17].ToString();
					temp = row[18].ToString();
					temp = row[19].ToString();
					temp = row[20].ToString();
					temp = row[21].ToString();
					temp = row[22].ToString();
					temp = row[23].ToString();
					temp = row[24].ToString();
					temp = row[25].ToString();					
				}
			}	
		}

		private void StringOrdinal(int iterations)
		{
			for (int i = 0; i < iterations; i++)
			{
				foreach (DataRow row in data.Tables[0].Rows)
				{				
					string temp = string.Empty;
					temp = row["a"].ToString();
					temp = row["b"].ToString();
					temp = row["c"].ToString();
					temp = row["d"].ToString();
					temp = row["e"].ToString();
					temp = row["f"].ToString();
					temp = row["g"].ToString();
					temp = row["h"].ToString();
					temp = row["i"].ToString();
					temp = row["j"].ToString();
					temp = row["k"].ToString();
					temp = row["l"].ToString();
					temp = row["m"].ToString();
					temp = row["n"].ToString();
					temp = row["o"].ToString();
					temp = row["p"].ToString();
					temp = row["q"].ToString();
					temp = row["r"].ToString();
					temp = row["s"].ToString();
					temp = row["t"].ToString();
					temp = row["u"].ToString();
					temp = row["v"].ToString();
					temp = row["w"].ToString();
					temp = row["x"].ToString();
					temp = row["y"].ToString();
					temp = row["z"].ToString();
				}
			}	
		}

		private void SmartOrdinal(int iterations)
		{
			for (int i = 0; i < iterations; i++)
			{
				Names.Init(data);

				foreach (DataRow row in data.Tables[0].Rows)
				{
					string temp = string.Empty;
					temp = row[Names.A].ToString();
					temp = row[Names.B].ToString();
					temp = row[Names.C].ToString();
					temp = row[Names.D].ToString();
					temp = row[Names.E].ToString();
					temp = row[Names.F].ToString();
					temp = row[Names.G].ToString();
					temp = row[Names.H].ToString();
					temp = row[Names.I].ToString();
					temp = row[Names.J].ToString();
					temp = row[Names.K].ToString();
					temp = row[Names.L].ToString();
					temp = row[Names.M].ToString();
					temp = row[Names.N].ToString();
					temp = row[Names.O].ToString();
					temp = row[Names.P].ToString();
					temp = row[Names.Q].ToString();
					temp = row[Names.R].ToString();
					temp = row[Names.S].ToString();
					temp = row[Names.T].ToString();
					temp = row[Names.U].ToString();
					temp = row[Names.V].ToString();
					temp = row[Names.W].ToString();
					temp = row[Names.X].ToString();
					temp = row[Names.Y].ToString();
					temp = row[Names.Z].ToString();
				}
			}	
		}		

		private void SmartOrdinal2(int iterations)
		{
			for (int i = 0; i < iterations; i++)
			{
				Names2.Init(data);

				foreach (DataRow row in data.Tables[0].Rows)
				{
					string temp = string.Empty;
					temp = row[Names2.A].ToString();
					temp = row[Names2.B].ToString();
					temp = row[Names2.C].ToString();
					temp = row[Names2.D].ToString();
					temp = row[Names2.E].ToString();
					temp = row[Names2.F].ToString();
					temp = row[Names2.G].ToString();
					temp = row[Names2.H].ToString();
					temp = row[Names2.I].ToString();
					temp = row[Names2.J].ToString();
					temp = row[Names2.K].ToString();
					temp = row[Names2.L].ToString();
					temp = row[Names2.M].ToString();
					temp = row[Names2.N].ToString();
					temp = row[Names2.O].ToString();
					temp = row[Names2.P].ToString();
					temp = row[Names2.Q].ToString();
					temp = row[Names2.R].ToString();
					temp = row[Names2.S].ToString();
					temp = row[Names2.T].ToString();
					temp = row[Names2.U].ToString();
					temp = row[Names2.V].ToString();
					temp = row[Names2.W].ToString();
					temp = row[Names2.X].ToString();
					temp = row[Names2.Y].ToString();
					temp = row[Names2.Z].ToString();
				}
			}	
		}		
	}

	public class Names
	{
		private static int[] rows = new int[26];
		private static int a,b,c,d,e,f,g,h,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
		private static bool isInitialized = false;

		public static void Init(DataSet ds)
		{	
			if (isInitialized) return;

			rows[0] = ds.Tables[0].Columns["a"].Ordinal;
			rows[1] = ds.Tables[0].Columns["b"].Ordinal;
			rows[2] = ds.Tables[0].Columns["c"].Ordinal;
			rows[3] = ds.Tables[0].Columns["d"].Ordinal;
			rows[4] = ds.Tables[0].Columns["e"].Ordinal;
			rows[5] = ds.Tables[0].Columns["f"].Ordinal;
			rows[6] = ds.Tables[0].Columns["g"].Ordinal;
			rows[7] = ds.Tables[0].Columns["h"].Ordinal;
			rows[8] = ds.Tables[0].Columns["i"].Ordinal;
			rows[9] = ds.Tables[0].Columns["j"].Ordinal;
			rows[10] = ds.Tables[0].Columns["k"].Ordinal;
			rows[11] = ds.Tables[0].Columns["l"].Ordinal;
			rows[12] = ds.Tables[0].Columns["m"].Ordinal;
			rows[13] = ds.Tables[0].Columns["n"].Ordinal;
			rows[14] = ds.Tables[0].Columns["o"].Ordinal;
			rows[15] = ds.Tables[0].Columns["p"].Ordinal;
			rows[16] = ds.Tables[0].Columns["q"].Ordinal;
			rows[17] = ds.Tables[0].Columns["r"].Ordinal;
			rows[18] = ds.Tables[0].Columns["s"].Ordinal;
			rows[19] = ds.Tables[0].Columns["t"].Ordinal;
			rows[20] = ds.Tables[0].Columns["u"].Ordinal;
			rows[21] = ds.Tables[0].Columns["v"].Ordinal;
			rows[22] = ds.Tables[0].Columns["w"].Ordinal;
			rows[23] = ds.Tables[0].Columns["x"].Ordinal;
			rows[24] = ds.Tables[0].Columns["y"].Ordinal;
			rows[25] = ds.Tables[0].Columns["z"].Ordinal;
			isInitialized = true;
		}

		public static int A { get {return rows[0];} }
		public static int B { get {return rows[1];} }
		public static int C { get {return rows[2];} }
		public static int D { get {return rows[3];} }
		public static int E { get {return rows[4];} }
		public static int F { get {return rows[5];} }
		public static int G { get {return rows[6];} }
		public static int H { get {return rows[7];} }
		public static int I { get {return rows[8];} }
		public static int J { get {return rows[9];} }
		public static int K { get {return rows[10];} }
		public static int L { get {return rows[11];} }
		public static int M { get {return rows[12];} }
		public static int N { get {return rows[13];} }
		public static int O { get {return rows[14];} }
		public static int P { get {return rows[15];} }
		public static int Q { get {return rows[16];} }
		public static int R { get {return rows[17];} }
		public static int S { get {return rows[18];} }
		public static int T { get {return rows[19];} }
		public static int U { get {return rows[20];} }
		public static int V { get {return rows[21];} }
		public static int W { get {return rows[22];} }
		public static int X { get {return rows[23];} }
		public static int Y { get {return rows[24];} }
		public static int Z { get {return rows[25];} }
	}

	public class Names2
	{		
		private static int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
		private static bool isInitialized = false;

		public static void Init(DataSet ds)
		{	
			if (isInitialized) return;

			a = ds.Tables[0].Columns["a"].Ordinal;
			b = ds.Tables[0].Columns["b"].Ordinal;
			c = ds.Tables[0].Columns["c"].Ordinal;
			d = ds.Tables[0].Columns["d"].Ordinal;
			e = ds.Tables[0].Columns["e"].Ordinal;
			f = ds.Tables[0].Columns["f"].Ordinal;
			g = ds.Tables[0].Columns["g"].Ordinal;
			h = ds.Tables[0].Columns["h"].Ordinal;
			i = ds.Tables[0].Columns["i"].Ordinal;
			j = ds.Tables[0].Columns["j"].Ordinal;
			k = ds.Tables[0].Columns["k"].Ordinal;
			l = ds.Tables[0].Columns["l"].Ordinal;
			m = ds.Tables[0].Columns["m"].Ordinal;
			n = ds.Tables[0].Columns["n"].Ordinal;
			o = ds.Tables[0].Columns["o"].Ordinal;
			p = ds.Tables[0].Columns["p"].Ordinal;
			q = ds.Tables[0].Columns["q"].Ordinal;
			r = ds.Tables[0].Columns["r"].Ordinal;
			s = ds.Tables[0].Columns["s"].Ordinal;
			t = ds.Tables[0].Columns["t"].Ordinal;
			u = ds.Tables[0].Columns["u"].Ordinal;
			v = ds.Tables[0].Columns["v"].Ordinal;
			w = ds.Tables[0].Columns["w"].Ordinal;
			x = ds.Tables[0].Columns["x"].Ordinal;
			y = ds.Tables[0].Columns["y"].Ordinal;
			z = ds.Tables[0].Columns["z"].Ordinal;
			
			isInitialized = true;
		}

		public static int A { get {return a;} }
		public static int B { get {return b;} }
		public static int C { get {return c;} }
		public static int D { get {return d;} }
		public static int E { get {return e;} }
		public static int F { get {return f;} }
		public static int G { get {return g;} }
		public static int H { get {return h;} }
		public static int I { get {return i;} }
		public static int J { get {return j;} }
		public static int K { get {return k;} }
		public static int L { get {return l;} }
		public static int M { get {return m;} }
		public static int N { get {return n;} }
		public static int O { get {return o;} }
		public static int P { get {return p;} }
		public static int Q { get {return q;} }
		public static int R { get {return r;} }
		public static int S { get {return s;} }
		public static int T { get {return t;} }
		public static int U { get {return u;} }
		public static int V { get {return v;} }
		public static int W { get {return w;} }
		public static int X { get {return x;} }
		public static int Y { get {return y;} }
		public static int Z { get {return z;} }
	}
}

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 Code Project Open License (CPOL)

Share

About the Author

jconwell

United States United States
I have been a professional developer since 1996. My experience comes from many different industries; Data Mining Software, Consulting, E-Commerce, Wholesale Operations, Clinical Software, Insurance, Energy.
 
I started programming in the military, trying to find better ways to analyze database data, eventually automating my entire job. Later, in college, I automated my way out of another job. This gave me the great idea to switch majors to the only thing that seemed natural…Programming!
Follow on   Twitter

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 1 May 2006
Article Copyright 2006 by jconwell
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid