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

Property Grid - Dynamic List ComboBox, Validation, and More

Rate me:
Please Sign up or sign in to vote.
4.57/5 (20 votes)
25 Sep 2009CPOL17 min read 167.6K   11.2K   90  
A PropertyGrid implementation showing how-to use, best practices, validation, and more.
using System;
using System.Reflection;

namespace Webbert.Utilites
{
	/// <summary>
	/// Utility class to access class fields/properties/methods via System.Reflection
	/// </summary>
	public static class Reflect
	{
		/// <summary>
		/// Create an instance of a class
		/// </summary>
		/// <param name="type">The type of class to instantiate</param>
		/// <param name="args">Arguments for the constructor</param>
		/// <returns>A class of the given type</returns>
		public static object CreateInstance( Type type, params object[] args )
		{
			return type.InvokeMember(
				null,
				BindingFlags.Public |
				BindingFlags.NonPublic |
				BindingFlags.CreateInstance |
				BindingFlags.Instance,
				null,
				null,
				args );
		}


		/// <summary>
		/// Set a field/property of a class that has been instantiated
		/// </summary>
		/// <param name="classInstance">Class instantiation that contains the field/property to set the value</param>
		/// <param name="dataMemberName">The field/property name</param>
		/// <param name="value">The value to set in the field/property</param>
		public static void SetDataMember( object classInstance, string dataMemberName, object value )
		{
			object[] args = { value };

			classInstance.GetType().InvokeMember(
				dataMemberName,
				BindingFlags.Public |
				BindingFlags.NonPublic |
				BindingFlags.SetField |
				BindingFlags.SetProperty |
				BindingFlags.Instance,
				null,
				classInstance,
				args );
		}


		/// <summary>
		/// Set a static field/property of a class
		/// </summary>
		/// <param name="type">The class type</param>
		/// <param name="dataMemberName">The field/property name</param>
		/// <param name="value">The value to set in the field/property</param>
		public static void SetStaticDataMember( Type type, string dataMemberName, object value )
		{
			object[] args = { value };

			type.InvokeMember(
				dataMemberName,
				BindingFlags.Public |
				BindingFlags.NonPublic |
				BindingFlags.SetField |
				BindingFlags.SetProperty |
				BindingFlags.Static,
				null,
				null,
				args );
		}


		/// <summary>
		/// Get the value of a field/property from an instantiated class
		/// </summary>
		/// <param name="classInstance">Class instantiation from which to obtain the field/property value</param>
		/// <param name="dataMemberName">The field/property name</param>
		/// <returns>The value from the field/property</returns>
		public static object GetDataMember( object classInstance, string dataMemberName )
		{
			return classInstance.GetType().InvokeMember(
				dataMemberName,
				BindingFlags.Public |
				BindingFlags.NonPublic |
				BindingFlags.GetField |
				BindingFlags.GetProperty |
				BindingFlags.Instance,
				null,
				classInstance,
				null );
		}


		/// <summary>
		/// Get the value from a static field/property from a class
		/// </summary>
		/// <param name="type">The class type</param>
		/// <param name="dataMemberName">The field/property name</param>
		/// <returns>The value from the field/property</returns>
		public static object GetStaticDataMember( Type type, string dataMemberName )
		{
			return type.InvokeMember(
				dataMemberName,
				BindingFlags.Public |
				BindingFlags.NonPublic |
				BindingFlags.GetField |
				BindingFlags.GetProperty |
				BindingFlags.Static,
				null,
				null,
				null
				);
		}


		/// <summary>
		/// Call a method of a class instantiation
		/// </summary>
		/// <param name="classInstance">Class instantiation that contains the method to call</param>
		/// <param name="methodName">The name of the method</param>
		/// <param name="args">Arguments for the method</param>
		/// <returns>The value from the method call</returns>
		public static object CallMethod( object classInstance, string methodName, params object[] args )
		{
			return classInstance.GetType().InvokeMember(
				methodName,
				BindingFlags.Public |
				BindingFlags.NonPublic |
				BindingFlags.InvokeMethod |
				BindingFlags.Instance,
				null,
				classInstance,
				args );
		}


		/// <summary>
		/// Call a static method of a class
		/// </summary>
		/// <param name="type">Class type that contains the method to call</param>
		/// <param name="memberName">The name of the method</param>
		/// <param name="args">Arguments for the method</param>
		/// <returns>The value from the method call</returns>
		public static object CallStaticMethod( Type type, string memberName, params object[] args )
		{
			return type.InvokeMember(
				memberName,
				BindingFlags.Public |
				BindingFlags.NonPublic |
				BindingFlags.InvokeMethod |
				BindingFlags.Static,
				null,
				null,
				args );
		}


		/// <summary>
		/// Call a field/property/method of a class instantiation
		/// </summary>
		/// <param name="classInstance">Class instantiation that contains the field/property/method to call</param>
		/// <param name="methodName">The name of the field/property/method</param>
		/// <param name="args">Arguments for the field/property/method</param>
		/// <returns>The value from the field/property/method call</returns>
		public static object CallGeneric( object classInstance, string methodName, params object[] args )
		{
			return classInstance.GetType().InvokeMember(
				methodName,
				BindingFlags.Public |
				BindingFlags.NonPublic |
				BindingFlags.GetField |
				BindingFlags.GetProperty |
				BindingFlags.InvokeMethod |
				BindingFlags.Instance,
				null,
				classInstance,
				args );
		}


		/// <summary>
		/// Call a static field/property/method of a class
		/// </summary>
		/// <param name="type">Class type that contains the field/property/method to call</param>
		/// <param name="memberName">The name of the field/property/method</param>
		/// <param name="args">Arguments for the field/property/method</param>
		/// <returns>The value from the field/property/method call</returns>
		public static object CallStaticGeneric( Type type, string memberName, params object[] args )
		{
			return type.InvokeMember(
				memberName,
				BindingFlags.Public |
				BindingFlags.NonPublic |
				BindingFlags.GetField |
				BindingFlags.GetProperty |
				BindingFlags.InvokeMethod |
				BindingFlags.Static,
				null,
				null,
				args );
		}
	}
}

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 (Senior) Webbert Solutions
United States United States
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.

Comments and Discussions