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

Runtime Object Editor

Rate me:
Please Sign up or sign in to vote.
4.93/5 (136 votes)
30 May 20068 min read 299.8K   10.9K   307  
A powerful window/object editor to be used at runtime that allows viewing/changing of properties and fields, method invocations, and object hierarchy navigation.
/* ****************************************************************************
 *  RuntimeObjectEditor
 * 
 * Copyright (c) 2005 Corneliu I. Tusnea
 * 
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the author be held liable for any damages arising from 
 * the use of this software.
 * Permission to use, copy, modify, distribute and sell this software for any 
 * purpose is hereby granted without fee, provided that the above copyright 
 * notice appear in all copies and that both that copyright notice and this 
 * permission notice appear in supporting documentation.
 * 
 * Corneliu I. Tusnea (corneliutusnea@yahoo.com.au)
 * www.acorns.com.au
 * ****************************************************************************/


using System;
using System.Reflection;

namespace RuntimeObjectEditor.Utils
{
	/// <summary>
	/// Summary description for FieldAccesor.
	/// </summary>
	public class FieldAccesor
	{
		private readonly string fieldName;
		private readonly Type targetType;

		private object target;
		private FieldInfo fieldInfo;

		private object value;

		public FieldAccesor(object target, string fieldName)
			: this(target.GetType(), target, fieldName)
		{
		}

		public FieldAccesor(Type targetType, string fieldName)
			: this(targetType, null, fieldName)
		{
		}

		public FieldAccesor(Type targetType, object target, string fieldName)
		{
			this.target = target;
			this.targetType = targetType;
			this.fieldName = fieldName;

			do
			{
				TryReadField(BindingFlags.Default);
				TryReadField(BindingFlags.Instance | BindingFlags.FlattenHierarchy);
				TryReadField(BindingFlags.Static | BindingFlags.FlattenHierarchy);

				TryReadField(BindingFlags.NonPublic | BindingFlags.Instance);

				TryReadField(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
				TryReadField(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.GetField);
				TryReadField(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField);

				TryReadField(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.IgnoreReturn | BindingFlags.Instance | BindingFlags.PutDispProperty | BindingFlags.PutRefDispProperty | BindingFlags.SetField | BindingFlags.Static);

				TryReadField(BindingFlags.NonPublic | BindingFlags.Static);
				TryReadField(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.GetField);
				TryReadField(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField);

				if (fieldInfo == null)
				{
					this.targetType = this.targetType.BaseType;
					if (this.targetType == typeof (object))
					{ // no chance
						break;
					}
				}

			} while (fieldInfo == null);
		}

		private void SearchForField(BindingFlags bindingFlags)
		{
			if (fieldInfo != null)
				return;

			FieldInfo[] allFields = targetType.GetFields(bindingFlags);
			foreach (FieldInfo field in allFields)
			{
				if (field.Name == this.fieldName)
				{
					this.fieldInfo = field;
					return;
				}
			}
		}


		private void TryReadField(BindingFlags bindingFlags)
		{
			if (fieldInfo != null)
				return;
			fieldInfo = targetType.GetField(fieldName, bindingFlags);
			if (fieldInfo == null)
			{
				SearchForField(bindingFlags);
			}
		}

		public void Save()
		{
			value = fieldInfo.GetValue(target);
		}

		public void Clear()
		{
			fieldInfo.SetValue(target, null);
		}

		public void Restore()
		{
			fieldInfo.SetValue(target, value);
		}

		public void Restore(object newValue)
		{
			fieldInfo.SetValue(target, newValue);
			this.value = newValue;
		}

		public object Target
		{
			get { return target; }
			set { this.target = value; }
		}

		public bool IsValid
		{
			get { return this.fieldInfo != null; }
		}

		public object Value
		{
			get { return this.value; }
		}
	}
}

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
Technical Lead OneSaas - Cloud Integrations Made Easy
Australia Australia

Comments and Discussions