Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

Use .NET Reflection APIs to facilitate get/set property

Rate me:
Please Sign up or sign in to vote.
2.63/5 (10 votes)
13 Jan 20051 min read 42.8K   488   12  
Dynamic access to .NET object's property/field.
//---------------------------------------------------------------------
//
// Copyright (C) 2005 Hongju Cao <hjcao_wei@hotmail.com>
//
// Permission to copy, use, modify, sell and distribute this software is 
// granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied warranty, 
// and with no claim as to its suitability for any purpose.
//
//---------------------------------------------------------------------

using System;
using System.Collections;

namespace First.Commons.Property
{
	/// <summary>
	/// DictionaryProxy ��ժҪ˵����
	/// </summary>
	internal sealed class DictionaryProxy : IDynaAccessProxy
	{
		private string[] _propertyNames = new string[0];

		public DictionaryProxy()
		{
			//
			// TODO: �ڴ˴���ӹ��캯���߼�
			//
		}

		#region IDynaAccessProxy methods

		public string[] GetPropertyNames(object target)
		{
			if (target is IDictionary) 
			{
				string [] propertyNames =  new string[((IDictionary) target).Count];
				((IDictionary) target).Keys.CopyTo(propertyNames,0);
				return propertyNames;
			} 
			else 
			{
				return _propertyNames;
			}
		}

		public void	SetProperty(object target , string propertyName , object propertyValue)
		{
			try 
			{
				if (target is IDictionary) 
				{
					((IDictionary) target)[propertyName] = propertyValue;
				} 
				else 
				{
					throw new DynaAccessException(target.GetType().Name + " class is not a IDictionary.");
				}
			}
			catch(Exception e)
			{
				throw new DynaAccessException(e);
			}
		}

		public object	GetProperty(object target , string propertyName)
		{
			object value = null;

			try 
			{
				if (target is IDictionary) 
				{
					value = ((IDictionary) target)[propertyName];
				} 
				else 
				{
					throw new DynaAccessException(target.GetType().Name + " class is not IDictionary.");
				}
			}
			catch(Exception e)
			{
				throw new DynaAccessException(e);
			}
			return value;
		}

		public bool HasProperty(object target , string propertyName)
		{
			if (target is IDictionary) 
			{
				try
				{
					return ((IDictionary) target).Contains(propertyName);
				}
				catch(Exception)
				{
					return false;
				}
			} 
			else 
			{
				return false;
			}
		}

		public Type GetPropertyType(object target , string propertyName)
		{
			object value = null;

			try 
			{
				if (target is IDictionary) 
				{
					value = ((IDictionary) target)[propertyName];
					if(value == null)
					{
						value  = new object();
					}

					return value.GetType();
				} 
				else 
				{
					throw new DynaAccessException(target.GetType().Name + " class is not a IDictionary.");
				}
			}
			catch(Exception e)
			{
				throw new DynaAccessException(e);
			}
		}

		public Type ProxyType
		{
			get
			{
				return typeof(System.Collections.IDictionary);
			}
		}

		#endregion
	}
}

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 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


Written By
Web Developer
China China
developer located in Tianjin, China.interests include C++/.NET/Java, etc. like coding.

Comments and Discussions