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
SystemObject
  System.Collections.GenericDictionaryString, String
    GSF.CollectionsSettingsCollection

Namespace: GSF.Collections
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.181-beta
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(IDictionaryString, String) Initializes a new instance of the SettingsCollection class.
Public methodSettingsCollection(IEqualityComparerString) Initializes a new instance of the SettingsCollection class.
Public methodSettingsCollection(IDictionaryString, String, IEqualityComparerString) Initializes a new instance of the SettingsCollection class.
Top
Properties
 NameDescription
Public propertyComparerGets the IEqualityComparerT that is used to determine equality of keys for the dictionary.
(Inherited from DictionaryString, String)
Public propertyCountGets the number of key/value pairs contained in the DictionaryTKey, TValue.
(Inherited from DictionaryString, String)
Public propertyItemGets or sets the value associated with the specified key.
(Inherited from DictionaryString, String)
Public propertyKeysGets a collection containing the keys in the DictionaryTKey, TValue.
(Inherited from DictionaryString, String)
Public propertyValidation Gets the ValidationService object used to perform validation on the Values.
Public propertyValuesGets a collection containing the values in the DictionaryTKey, TValue.
(Inherited from DictionaryString, String)
Top
Methods
 NameDescription
Public methodAddAdds the specified key and value to the dictionary.
(Inherited from DictionaryString, String)
Public methodClearRemoves all keys and values from the DictionaryTKey, TValue.
(Inherited from DictionaryString, String)
Public methodContainsKeyDetermines whether the DictionaryTKey, TValue contains the specified key.
(Inherited from DictionaryString, String)
Public methodContainsValueDetermines whether the DictionaryTKey, TValue contains a specific value.
(Inherited from DictionaryString, 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 DictionaryTKey, TValue.
(Inherited from DictionaryString, String)
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetObjectDataImplements the ISerializable interface and returns the data needed to serialize the DictionaryTKey, TValue instance.
(Inherited from DictionaryString, 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 DictionaryString, String)
Public methodRemoveRemoves the value with the specified key from the DictionaryTKey, TValue.
(Inherited from DictionaryString, String)
Public methodToString Gets the String representation of SettingsCollection.
(Overrides ObjectToString)
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 DictionaryString, String)
Top
Operators
 NameDescription
Public operatorStatic member(SettingsCollection to String) Implicitly converts SettingsCollection to a String.
Public operatorStatic member(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 MethodGetEnumValueOrDefaultT 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