#region Copyright (c) 2011-2023 Technosoftware GmbH. All rights reserved //----------------------------------------------------------------------------- // Copyright (c) 2011-2023 Technosoftware GmbH. All rights reserved // Web: https://www.technosoftware.com // // The source code in this file is covered under a dual-license scenario: // - Owner of a purchased license: SCLA 1.0 // - GPL V3: everybody else // // SCLA license terms accompanied with this source code. // See SCLA 1.0: https://technosoftware.com/license/Source_Code_License_Agreement.pdf // // GNU General Public License as published by the Free Software Foundation; // version 3 of the License are accompanied with this source code. // See https://technosoftware.com/license/GPLv3License.txt // // This source code is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. //----------------------------------------------------------------------------- #endregion Copyright (c) 2011-2023 Technosoftware GmbH. All rights reserved #region Using Directives using System; using System.Collections; using System.Runtime.Serialization; #endregion namespace Technosoftware.DaAeHdaClient { /// /// A read only dictionary class which can be used to expose arrays as properties of classes. /// [Serializable] public class OpcReadOnlyDictionary : IDictionary, ISerializable { #region Protected Interface /// ///Creates a collection that wraps the specified array instance. /// protected OpcReadOnlyDictionary(Hashtable dictionary) { Dictionary = dictionary; } /// /// The array instance exposed by the collection. /// protected virtual Hashtable Dictionary { get => dictionary_; set { dictionary_ = value; if (dictionary_ == null) { dictionary_ = new Hashtable(); } } } #endregion #region ISerializable Members /// /// A set of names for fields used in serialization. /// private class Names { internal const string Count = "CT"; internal const string Key = "KY"; internal const string Value = "VA"; } /// /// Construct a server by de-serializing its OpcUrl from the stream. /// protected OpcReadOnlyDictionary(SerializationInfo info, StreamingContext context) { var count = (int)info.GetValue(Names.Count, typeof(int)); dictionary_ = new Hashtable(); for (var ii = 0; ii < count; ii++) { var key = info.GetValue(Names.Key + ii.ToString(), typeof(object)); var value = info.GetValue(Names.Value + ii.ToString(), typeof(object)); if (key != null) { dictionary_[key] = value; } } } /// /// Serializes a server into a stream. /// public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue(Names.Count, dictionary_.Count); var ii = 0; var enumerator = dictionary_.GetEnumerator(); while (enumerator.MoveNext()) { info.AddValue(Names.Key + ii.ToString(), enumerator.Key); info.AddValue(Names.Value + ii.ToString(), enumerator.Value); ii++; } } #endregion #region IDictionary Members /// /// Gets a value indicating whether the IDictionary is read-only. /// public virtual bool IsReadOnly => true; /// /// Returns an IDictionaryEnumerator for the IDictionary. /// public virtual IDictionaryEnumerator GetEnumerator() { return dictionary_.GetEnumerator(); } /// /// Gets or sets the element with the specified key. /// public virtual object this[object key] { get => dictionary_[key]; set => throw new InvalidOperationException(ReadOnlyDictionary); } /// /// Removes the element with the specified key from the IDictionary. /// public virtual void Remove(object key) { throw new InvalidOperationException(ReadOnlyDictionary); } /// /// Determines whether the IDictionary contains an element with the specified key. /// public virtual bool Contains(object key) { return dictionary_.Contains(key); } /// /// Removes all elements from the IDictionary. /// public virtual void Clear() { throw new InvalidOperationException(ReadOnlyDictionary); } /// /// Gets an ICollection containing the values in the IDictionary. /// public virtual ICollection Values => dictionary_.Values; /// /// Adds an element with the provided key and value to the IDictionary. /// public void Add(object key, object value) { throw new InvalidOperationException(ReadOnlyDictionary); } /// /// Gets an ICollection containing the keys of the IDictionary. /// public virtual ICollection Keys => dictionary_.Keys; /// /// Gets a value indicating whether the IDictionary has a fixed size. /// public virtual bool IsFixedSize => false; #endregion #region ICollection Members /// /// Indicates whether access to the ICollection is synchronized (thread-safe). /// public virtual bool IsSynchronized => false; /// /// Gets the number of objects in the collection. /// public virtual int Count => dictionary_.Count; /// /// Copies the objects to an Array, starting at a the specified index. /// /// The one-dimensional Array that is the destination for the objects. /// The zero-based index in the Array at which copying begins. public virtual void CopyTo(Array array, int index) { if (dictionary_ != null) { dictionary_.CopyTo(array, index); } } /// /// Indicates whether access to the ICollection is synchronized (thread-safe). /// public virtual object SyncRoot => this; #endregion #region IEnumerable Members /// /// Returns an enumerator that can iterate through a collection. /// /// An IEnumerator that can be used to iterate through the collection. IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion #region ICloneable Members /// /// Creates a deep copy of the collection. /// public virtual object Clone() { var clone = (OpcReadOnlyDictionary)MemberwiseClone(); // clone contents of hashtable. var dictionary = new Hashtable(); var enumerator = dictionary_.GetEnumerator(); while (enumerator.MoveNext()) { dictionary.Add(OpcConvert.Clone(enumerator.Key), OpcConvert.Clone(enumerator.Value)); } clone.dictionary_ = dictionary; // return clone. return clone; } #endregion #region Private Members private Hashtable dictionary_ = new Hashtable(); private const string ReadOnlyDictionary = "Cannot change the contents of a read-only dictionary"; #endregion } }