Click here to Skip to main content
15,885,899 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 542.1K   7.5K   489  
Simplifying the .NET User Configuration for Windows Forms and WPF
// -- FILE ------------------------------------------------------------------
// name       : SettingCollection.cs
// created    : Jani Giannoudis - 2008.04.25
// language   : c#
// environment: .NET 2.0
// --------------------------------------------------------------------------
using System;
using System.Collections;
using System.Reflection;

namespace Itenso.Configuration
{

	// ------------------------------------------------------------------------
	public sealed class SettingCollection : IEnumerable
	{

		// ----------------------------------------------------------------------
		public SettingCollection( ApplicationSettings applicationSettings )
		{
			if ( applicationSettings == null )
			{
				throw new ArgumentNullException( "applicationSettings" );
			}

			this.applicationSettings = applicationSettings;
		} // SettingCollection

		// ----------------------------------------------------------------------
		public ApplicationSettings ApplicationSettings
		{
			get { return this.applicationSettings; }
		} // ApplicationSettings

		// ----------------------------------------------------------------------
		public int Count
		{
			get { return this.settings.Count; }
		} // Count

		// ----------------------------------------------------------------------
		public bool HasChanges
		{
			get 
			{
				foreach ( ISetting setting in this.settings )
				{
					if ( setting.HasChanged )
					{
						return true;
					}
				}
				return false;
			}
		} // HasChanges

		// ----------------------------------------------------------------------
		public IEnumerator GetEnumerator()
		{
			return this.settings.GetEnumerator();
		} // GetEnumerator

		// ----------------------------------------------------------------------
		public bool Contains( ISetting setting )
		{
			if ( setting == null )
			{
				throw new ArgumentNullException( "setting" );
			}
			return this.settings.Contains( setting );
		} // Contains

		// ----------------------------------------------------------------------
		public void Add( ISetting setting )
		{
			if ( setting == null )
			{
				throw new ArgumentNullException( "setting" );
			}
			setting.ApplicationSettings = this.applicationSettings;
			this.settings.Add( setting );
		} // Add

		// ----------------------------------------------------------------------
		public void AddAll( object obj )
		{
			if ( obj == null )
			{
				throw new ArgumentNullException( "obj" );
			}

			// field settings
			FieldInfo[] fieldInfos = obj.GetType().GetFields(
				BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic );
			foreach ( FieldInfo fieldInfo in fieldInfos )
			{
				FieldSettingAttribute[] settingAttributes = (FieldSettingAttribute[])fieldInfo.GetCustomAttributes( typeof( FieldSettingAttribute ), true );
				if ( settingAttributes.Length <= 0 )
				{
					continue;
				}

				FieldSettingAttribute settingAttribute = settingAttributes[ 0 ];
				string settingName = settingAttribute.Name;
				if ( string.IsNullOrEmpty( settingName ) )
				{
					settingName = fieldInfo.Name;
				}
				object defaultValue = settingAttribute.DefaultValue;
				FieldSetting fieldSetting = new FieldSetting(
					settingName, obj, fieldInfo.Name, defaultValue );
				Add( fieldSetting );
			}

			// property settings
			PropertyInfo[] propertyInfos = obj.GetType().GetProperties(
				BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic );
			foreach ( PropertyInfo propertyInfo in propertyInfos )
			{
				PropertySettingAttribute[] settingAttributes = (PropertySettingAttribute[])propertyInfo.GetCustomAttributes( typeof( PropertySettingAttribute ), true );
				if ( settingAttributes.Length <= 0 )
				{
					continue;
				}

				PropertySettingAttribute settingAttribute = settingAttributes[ 0 ];
				string settingName = settingAttribute.Name;
				if ( string.IsNullOrEmpty( settingName ) )
				{
					settingName = propertyInfo.Name;
				}
				object defaultValue = settingAttribute.DefaultValue;
				PropertySetting propertySetting = new PropertySetting(
					settingName, obj, propertyInfo.Name, defaultValue );
				Add( propertySetting );
			}
		} // AddAll

		// ----------------------------------------------------------------------
		public void Remove( ISetting setting )
		{
			if ( setting == null )
			{
				throw new ArgumentNullException( "setting" );
			}
			this.settings.Remove( setting );
		} // Remove

		// ----------------------------------------------------------------------
		public void Clear()
		{
			this.settings.Clear();
		} // Clear

		// ----------------------------------------------------------------------
		// members
		private readonly ArrayList settings = new ArrayList();
		private readonly ApplicationSettings applicationSettings;

	} // class SettingCollection

} // 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