Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / Windows Forms

User Settings Applied

Rate me:
Please Sign up or sign in to vote.
4.95/5 (136 votes)
5 Sep 2013CPOL10 min read 542K   7.5K   489  
Simplifying the .NET User Configuration for Windows Forms and WPF
// -- FILE ------------------------------------------------------------------
// name       : FieldSetting.cs
// created    : Jani Giannoudis - 2008.04.25
// language   : c#
// environment: .NET 2.0
// --------------------------------------------------------------------------
using System;
using System.Reflection;

namespace Itenso.Configuration
{

	// ------------------------------------------------------------------------
	public class FieldSetting : ValueSettingBase
	{

		// ----------------------------------------------------------------------
		public FieldSetting( object component, FieldInfo fieldInfo ) :
			this( fieldInfo.Name, component, fieldInfo )
		{
		} // FieldSetting

		// ----------------------------------------------------------------------
		public FieldSetting( string name, object component, FieldInfo fieldInfo ) :
			this( name, component, fieldInfo, null )
		{
		} // FieldSetting

		// ----------------------------------------------------------------------
		public FieldSetting( string name, object component, FieldInfo fieldInfo, object defaultValue ) :
			base( name, defaultValue )
		{
			if ( component == null )
			{
				throw new ArgumentNullException( "component" );
			}
			if ( fieldInfo == null )
			{
				throw new ArgumentNullException( "fieldInfo" );
			}

			this.component = component;
			this.fieldInfo = fieldInfo;
		} // FieldSetting

		// ----------------------------------------------------------------------
		public FieldSetting( object component, string fieldName ) :
			this( fieldName, component, fieldName )
		{
		} // FieldSetting
		
		// ----------------------------------------------------------------------
		public FieldSetting( string name, object component, string fieldName ) :
			this( name, component, fieldName, null )
		{
		} // FieldSetting

		// ----------------------------------------------------------------------
		public FieldSetting( string name, object component, string fieldName, object defaultValue ) :
			base( name, defaultValue )
		{
			if ( component == null )
			{
				throw new ArgumentNullException( "component" );
			}
			if ( string.IsNullOrEmpty( fieldName ) )
			{
				throw new ArgumentNullException( "fieldName" );
			}

			this.component = component;
			this.fieldInfo = component.GetType().GetField( fieldName,
				BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic );
			if ( fieldInfo == null )
			{
				throw new ArgumentException( "missing setting field: " + fieldName );
			}
			if ( fieldInfo.IsInitOnly ) // readonly field
			{
				throw new ArgumentException( "setting field '" + fieldName + "' is readonly" );
			}
		} // FieldSetting

		// ----------------------------------------------------------------------
		public FieldInfo FieldInfo
		{
			get { return this.fieldInfo; }
		} // FieldInfo

		// ----------------------------------------------------------------------
		public string FieldName
		{
			get { return this.fieldInfo.Name; }
		} // FieldName

		// ----------------------------------------------------------------------
		public object Component
		{
			get { return this.component; }
		} // Component

		// ----------------------------------------------------------------------
		public override object OriginalValue
		{
			get { return LoadValue( Name, this.fieldInfo.FieldType, SerializeAs, DefaultValue ); }
		} // OriginalValue

		// ----------------------------------------------------------------------
		public override object Value
		{
			get { return this.fieldInfo.GetValue( this.component ); }
			set { this.fieldInfo.SetValue( this.component, value ); }
		} // Value

		// ----------------------------------------------------------------------
		public override void Load()
		{
			try
			{
				object originalValue = OriginalValue;
				if ( originalValue == null && LoadUndefinedValue == false )
				{
					return;
				}
				Value = originalValue;
			}
			catch
			{
				if ( ThrowOnErrorLoading )
				{
					throw;
				}
			}
		} // Load

		// ----------------------------------------------------------------------
		public override void Save()
		{
			try
			{
				object value = Value;
				if ( value == null && SaveUndefinedValue == false )
				{
					return;
				}
				SaveValue( Name, this.fieldInfo.FieldType, SerializeAs, value, DefaultValue );
			}
			catch
			{
				if ( ThrowOnErrorSaving )
				{
					throw;
				}
			}
		} // Save

		// ----------------------------------------------------------------------
		// members
		private readonly object component;
		private readonly FieldInfo fieldInfo;

	} // class FieldSetting

} // namespace Itenso.Configuration
// -- EOF -------------------------------------------------------------------

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)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions