Click here to Skip to main content
15,891,424 members
Articles / Programming Languages / C#

Populating a PropertyGrid using Reflection.Emit

Rate me:
Please Sign up or sign in to vote.
4.62/5 (25 votes)
30 Jan 2004Public Domain3 min read 180.6K   1.2K   71  
How to create a class at runtime so you can use its properties in a PropertyGrid.
/* All code is written my me,Ben Ratzlaff and is available for any free use except where noted. 
 *I assume no responsibility for how anyone uses the information available*/

using System;
using System.Collections;

namespace CustomPropGrid
{
	/// <summary>
	/// A wrapper around a Hashtable for Setting objects. Setting objects are intended to use with the CustomPropertyGrid
	/// </summary>
	public class Settings
	{
		private Hashtable settings;		
		public Settings()
		{
			settings = new Hashtable();
		}

		/// <summary>
		/// Get the key collection for this Settings object. Every key is a string
		/// </summary>
		public ICollection Keys
		{
			get{return settings.Keys;}
		}

		/// <summary>
		/// Get/Set the Setting object tied to the input string
		/// </summary>
		public Setting this[string key]
		{
			get{return (Setting)settings[key];}
			set{settings[key]=value;value.Name=key;}
		}

		/// <summary>
		/// Gets the Setting object tied to the string. If there is no Setting object, one will be created with the defaultValue
		/// </summary>
		/// <param name="key">The name of the setting object</param>
		/// <param name="defaultvalue">if there is no Setting object tied to the string, a Setting will be created with this as its Value</param>
		/// <returns>The Setting object tied to the string</returns>
		public Setting GetSetting(string key, object defaultvalue)
		{
			if(settings[key]==null)
			{
				settings[key]=new Setting(defaultvalue,null,null);
				((Setting)settings[key]).Name=key;
			}

			return (Setting)settings[key];
		}
	}
}

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 A Public Domain dedication


Written By
Software Developer (Senior)
United States United States
Graduate of the Computer Science department at the University of Arizona

Comments and Discussions