Click here to Skip to main content
15,886,796 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 311.3K   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: The PropertyAccessor class uses this interface 
//		for creating a type at runtime for accessing an individual
//		property on a target object.
//
// *** 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;

namespace FastDynamicPropertyAccessor
{
	/// <summary>
	/// The IPropertyAccessor interface defines a property
	/// accessor.
	/// </summary>
	public interface IPropertyAccessor
	{
		/// <summary>
		/// Gets the value stored in the property for 
		/// the specified target.
		/// </summary>
		/// <param name="target">Object to retrieve
		/// the property from.</param>
		/// <returns>Property value.</returns>
		object Get(object target);

		/// <summary>
		/// Sets the value for the property of
		/// the specified target.
		/// </summary>
		/// <param name="target">Object to set the
		/// property on.</param>
		/// <param name="value">Property value.</param>
		void Set(object target, object value);
	}
}

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