Return to Snippet

Revision: 30082
at August 7, 2010 05:01 by kdrucker

Initial Code
ExampleClass example = new ExampleClass();

object oExampleContainer = example;
updateObjectProperty(ref oExampleContainer, "exampleProperty", 12345, new List<string>());

private void updateObjectProperty(ref object thisObject, string propertyName, object value, List<string> excludeList)
	if (!excludeList.Contains(propertyName))
		if (thisObject != null)
			Type thisType = thisObject.GetType();
			PropertyInfo[] propertyInfos = thisType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
			foreach (PropertyInfo info in propertyInfos)
				if (info.Name.ToUpper() == propertyName.ToUpper())
					info.SetValue(thisObject, value, null);

Initial URL

Initial Description
Since your domain object is passed in as a ref parameter, it cannot be cast to object, you must box it prior to calling the method. The caller of the method must also ensure that the System.Type of the value parameter matches the type expected by the property being updated.

Initial Title
Update domain object properties using .NET Reflection.

Initial Tags

Initial Language