Click here to Skip to main content
15,898,010 members
Articles / Programming Languages / MSIL

Fast Dynamic Property Access with C#

Rate me:
Please Sign up or sign in to vote.
4.89/5 (49 votes)
22 Mar 2005CPOL2 min read 312.6K   2.3K   153  
Reflecting on Properties is nice, but often it can be too slow. This article describes an alternative method for dynamic property access.
//
// Author: James Nies
// Date: 3/22/2005
// Description: Class for testing the PropertyAccessor.
//
// *** This code was written by James Nies and has been provided to you, ***
// *** free of charge, for your use.  I assume no responsibility for any ***
// *** undesired events resulting from the use of this code or the		 ***
// *** information that has been provided with it .						 ***
//

using System;
using System.Collections;

namespace FastDynamicPropertyAccessor
{
	/// <summary>
	/// PropertyAccessorTestObject.
	/// </summary>
	public class PropertyAccessorTestObject
	{
		public PropertyAccessorTestObject()
		{
		}

		public int Int
		{
			get
			{
				return this.mInt;
			}
			set
			{
				this.mInt = value;
			}
		}

		public string String
		{
			get
			{
				return this.mString;
			}
			set
			{	
				this.mString = value;
			}
		}

		public sbyte Sbyte
		{
			get
			{
				return this.mSbyte;
			}
			set
			{
				this.mSbyte = value;
			}
		}

		public byte Byte
		{
			get
			{
				return this.mByte;
			}
			set
			{
				this.mByte = value;
			}
		}

		public char Char
		{
			get
			{
				return this.mChar;
			}
			set
			{
				this.mChar = value;
			}
		}

		public short Short
		{
			get
			{
				return this.mShort;
			}
			set
			{
				this.mShort = value;
			}
		}

		public ushort UShort
		{
			get
			{
				return this.mUshort;
			}
			set
			{
				this.mUshort = value;
			}
		}

		public long Long
		{
			get
			{
				return this.mLong;
			}
			set
			{
				this.mLong = value;
			}
		}

		public ulong ULong
		{
			get
			{
				return this.mUlong;
			}
			set
			{
				this.mUlong = value;
			}
		}

		public bool Bool
		{
			get
			{
				return this.mBool;
			}
			set
			{
				this.mBool = value;
			}
		}

		public double Double
		{
			get
			{
				return this.mDouble;
			}
			set
			{
				this.mDouble = value;
			}
		}

		public float Float
		{
			get
			{
				return this.mFloat;
			}
			set
			{
				this.mFloat = value;
			}
		}

		public DateTime DateTime
		{
			get
			{
				return this.mDateTime;
			}
			set
			{
				this.mDateTime = value;
			}
		}

		public decimal Decimal
		{
			get
			{
				return this.mDecimal;
			}
			set
			{
				this.mDecimal = value;
			}
		}

		public IList List
		{
			get
			{
				return this.mList;
			}
			set
			{
				this.mList = value;
			}
		}

		public int ReadOnlyInt
		{
			get
			{
				return this.mReadOnlyInt;
			}
		}

		public int WriteOnlyInt
		{
			set
			{
				this.mWriteOnlyInt = value;	
			}
		}

		private int mInt;
		private string mString;
		private sbyte mSbyte;
		private byte mByte;
		private char mChar;
		private short mShort;
		private ushort mUshort;
		private long mLong;
		private ulong mUlong;
		private bool mBool;
		private double mDouble;
		private float mFloat;
		private DateTime mDateTime;
		private decimal mDecimal;
		private IList mList;
		private int mReadOnlyInt = 0;
		private int mWriteOnlyInt;
	}
}

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)


Written By
Software Developer Astral Softworks, LLC
United States United States
James Nies is a graduate of the University of Wyoming's Department of Computer Science. He is currently employed as a software developer and has been programming in C#, his favorite language, for the last 5 years. James recently formed a small software development and web design company, Astral Softworks, LLC.



Comments and Discussions