Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Property Grid - Dynamic List ComboBox, Validation, and More

, 25 Sep 2009
A PropertyGrid implementation showing how-to use, best practices, validation, and more.
propertygrid-demo.zip
PropertyGrid - Demo
CarApplication.exe
CustomControls.dll
DataModel.dll
Utilites.dll
propertygrid-source.zip
PropertyGrid-Source
CarApplication
Converters
Properties
Settings.settings
View
CustomControls
ComboBox
Data
Properties
Rule
DataModel
CarModel
Cars
Wheels
Falken
Kumho
PersonModel
Properties
Utilites
Properties
propertygrid_-_demo.zip
CarApplication.exe
CustomControls.dll
DataModel.dll
Utilites.dll
propertygrid_-_source.zip
PropertyGrid
CarApplication
Converters
Properties
Settings.settings
View
CarDomainModel
Cars
Properties
Wheels
Falken
Kumho
CustomControls
ComboBox
Data
Properties
Rule
DataModel
CarModel
Cars
Wheels
Falken
Kumho
Properties
PersonModel
PersonDomainModel
Properties
PropertyGrid.suo
Utilites
Properties
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)

Share

About the Author

Dave Elliott
Software Developer (Senior) Webbert Solutions
United States United States
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 25 Sep 2009
Article Copyright 2008 by Dave Elliott
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid