Click or drag to resize

SettingsCollection Class

A collection of settings that can be represented as a string of key-value pairs for easy persistence and also provide built-in validation support for the settings.
Inheritance Hierarchy
System.Object
  System.Collections.Generic.Dictionary<String, String>
    GSF.Collections.SettingsCollection

Namespace: GSF.Collections
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.245-beta+94213ae9d4d0aa7a2c48e5398b12d267b99f868a
Syntax
[SerializableAttribute]
public class SettingsCollection : Dictionary<string, string>
View Source

The SettingsCollection type exposes the following members.

Constructors
 NameDescription
Public methodSettingsCollection() Initializes a new instance of the SettingsCollection class.
Public methodSettingsCollection(IDictionary<String, String>) Initializes a new instance of the SettingsCollection class.
Public methodSettingsCollection(IEqualityComparer<String>) Initializes a new instance of the SettingsCollection class.
Public methodSettingsCollection(IDictionary<String, String>, IEqualityComparer<String>) Initializes a new instance of the SettingsCollection class.
Top
Properties
 NameDescription
Public propertyComparerGets the IEqualityComparer<T> that is used to determine equality of keys for the dictionary.
(Inherited from Dictionary<String, String>)
Public propertyCountGets the number of key/value pairs contained in the Dictionary<TKey, TValue>.
(Inherited from Dictionary<String, String>)
Public propertyItemGets or sets the value associated with the specified key.
(Inherited from Dictionary<String, String>)
Public propertyKeysGets a collection containing the keys in the Dictionary<TKey, TValue>.
(Inherited from Dictionary<String, String>)
Public propertyValidation Gets the ValidationService object used to perform validation on the Values.
Public propertyValuesGets a collection containing the values in the Dictionary<TKey, TValue>.
(Inherited from Dictionary<String, String>)
Top
Methods
 NameDescription
Public methodAddAdds the specified key and value to the dictionary.
(Inherited from Dictionary<String, String>)
Public methodClearRemoves all keys and values from the Dictionary<TKey, TValue>.
(Inherited from Dictionary<String, String>)
Public methodContainsKeyDetermines whether the Dictionary<TKey, TValue> contains the specified key.
(Inherited from Dictionary<String, String>)
Public methodContainsValueDetermines whether the Dictionary<TKey, TValue> contains a specific value.
(Inherited from Dictionary<String, String>)
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
Public methodGetEnumeratorReturns an enumerator that iterates through the Dictionary<TKey, TValue>.
(Inherited from Dictionary<String, String>)
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetObjectDataImplements the ISerializable interface and returns the data needed to serialize the Dictionary<TKey, TValue> instance.
(Inherited from Dictionary<String, String>)
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Public methodOnDeserializationImplements the ISerializable interface and raises the deserialization event when the deserialization is complete.
(Inherited from Dictionary<String, String>)
Public methodRemoveRemoves the value with the specified key from the Dictionary<TKey, TValue>.
(Inherited from Dictionary<String, String>)
Public methodToString Gets the String representation of SettingsCollection.
(Overrides Object.ToString())
Public methodTryAdd Adds an element with the specified key and value if an element is not present with the specified key.
Public methodTryGetValueGets the value associated with the specified key.
(Inherited from Dictionary<String, String>)
Top
Operators
 NameDescription
Public operatorStatic memberImplicit(SettingsCollection to String) Implicitly converts SettingsCollection to a String.
Public operatorStatic memberImplicit(String to SettingsCollection) Implicitly converts String to SettingsCollection.
Top
Extension Methods
 NameDescription
Public Extension MethodGetEnumValueOrDefault Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodGetEnumValueOrDefault<T> Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Top
Example
This example shows how to use SettingsCollection for key-value pair type settings and apply validation to them:
C#
using System;
using System.Collections.Generic;
using GSF.Collections;
using GSF.Validation;

class Program
{
    static void Main(string[] args)
    {
        // Initialize settings.
        SettingsCollection settings = "Server=localhost;Port=5000";
        // Add validation rules.
        settings.Validation.AddValidation("Server", new NonNullStringValidator());
        settings.Validation.AddValidation("Port", new NonNullStringValidator());
        settings.Validation.AddValidation("Port", new NumericRangeValidator(1000, 2000));

        // Validate settings.
        string errors;
        if (!settings.Validation.Validate(out errors))
        {
            // Show validation errors.
            Console.WriteLine(string.Format("Settings: {0}\r\n", settings));
            Console.WriteLine(errors);
        }
        else
        {
            // Show stored settings.
            foreach (KeyValuePair<string, string> setting in settings)
            {
                Console.WriteLine(string.Format("Key={0}; Value={1}", setting.Key, setting.Value));
            }
        }

        Console.ReadLine();
    }
}
See Also