Esempio n. 1
0
        /// <summary>
        /// Gets the value with the given name, or the given default value if not found.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name">The name.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public T GetValue <T>(string name, T defaultValue)
        {
            NamedValue nv = Get(name, false);

            if (nv == null)
            {
                return(defaultValue);
            }
            return(nv.AsValue <T>());
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the value with the given name. Throws an exception if a mandatory name is not found.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name">The name.</param>
        /// <param name="isMandatory">if set to <c>true</c> [is mandatory].</param>
        /// <returns></returns>
        public T GetValue <T>(string name, bool isMandatory)
        {
            NamedValue nv = Get(name, isMandatory);

            if (nv == null)
            {
                return(default(T));
            }
            return(nv.AsValue <T>());
        }
Esempio n. 3
0
        /// <summary>
        /// Finds the array value with the given name, or returns the default provided.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name">The name of the array.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public T[] GetArray <T>(string name, T[] defaultValue)
        {
            NamedValue nv = Get(name, false);

            if (nv == null)
            {
                return(defaultValue);
            }
            return(nv.AsArray <T>());
        }
Esempio n. 4
0
 /// <summary>
 /// Constructs a modifiable <see cref="NamedValueSet"/> with a single named value.
 /// </summary>
 /// <param name="nv">The NVS.</param>
 public NamedValueSet(NamedValue nv)
 {
     if (nv != null)
     {
         _dict.Locked(dict =>
         {
             dict[nv.Name.ToLowerInvariant()] = nv;
         });
     }
 }
Esempio n. 5
0
        public string GetString(string name, string defaultValue)
        {
            NamedValue nv = Get(name, false);

            if (nv == null)
            {
                return(defaultValue);
            }
            return(nv.AsValue <string>());
        }
Esempio n. 6
0
        /// <summary>
        /// Finds the string value with the given name. Throws an exception if a mandatory name is not found.
        /// </summary>
        /// <param name="name">The name of the value.</param>
        /// <param name="isMandatory">The isMandatory flag. </param>
        /// <returns></returns>
        public string GetString(string name, bool isMandatory)
        {
            NamedValue nv = Get(name, isMandatory);

            if (nv == null)
            {
                return("");
            }
            return(nv.AsValue <string>());
        }
Esempio n. 7
0
 public void Add <T>(IDictionary <string, T> newValues)
 {
     _dict.Locked(dict =>
     {
         foreach (string name in newValues.Keys)
         {
             var nv = new NamedValue(name, newValues[name]);
             dict[nv.Name.ToLowerInvariant()] = nv;
         }
     });
 }
Esempio n. 8
0
        /// <summary>
        /// Gets the nullable typed value with the given name, or null if not found.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public T?GetNullable <T>(string name) where T : struct
        {
            T?         result = null;
            NamedValue nv     = Get(name, false);

            if (nv != null)
            {
                result = nv.AsValue <T>();
            }
            return(result);
        }
Esempio n. 9
0
        /// <summary>
        /// Finds the NameValue object with the given name. Throws an exception if a mandatory name is not found.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="isMandatory">if set to <c>true</c> [is mandatory].</param>
        /// <returns></returns>
        public NamedValue Get(string name, bool isMandatory)
        {
            NamedValue result = null;
            string     key    = name.ToLowerInvariant();

            _dict.Locked(dict => dict.TryGetValue(key, out result));
            // mandatory value - fail if not found
            if (isMandatory && (result == null))
            {
                throw new ArgumentException("Mandatory value not set!", name);
            }
            return(result);
        }
Esempio n. 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NamedValueSet"/> class.
 /// </summary>
 /// <param name="dictionary">The dictionary.</param>
 public NamedValueSet(IDictionary dictionary)
 {
     if (dictionary != null)
     {
         _dict.Locked(dict =>
         {
             foreach (DictionaryEntry dictionaryEntry in dictionary)
             {
                 var nv = new NamedValue(dictionaryEntry.Key.ToString(), dictionaryEntry.Value);
                 dict[nv.Name.ToLowerInvariant()] = nv;
             }
         });
     }
 }
Esempio n. 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NamedValueSet"/> class.
 /// </summary>
 /// <param name="dictionary">The dictionary.</param>
 public NamedValueSet(IEnumerable <KeyValuePair <string, string> > dictionary)
 {
     if (dictionary != null)
     {
         _dict.Locked(dict =>
         {
             foreach (KeyValuePair <string, string> kvp in dictionary)
             {
                 var nv = new NamedValue(kvp.Key, kvp.Value);
                 dict[nv.Name.ToLowerInvariant()] = nv;
             }
         });
     }
 }
Esempio n. 12
0
 public void Set(NamedValue nv)
 {
     if (null == nv)
     {
         return;
     }
     if (_frozen)
     {
         throw new InvalidOperationException("Cannot modify frozen object");
     }
     _dict.Locked(dict =>
     {
         dict[nv.Name.ToLowerInvariant()] = nv;
     });
 }
Esempio n. 13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="valueString"></param>
        /// <returns></returns>
        public object DeserialiseVector(string valueString)
        {
            // special case - byte arrays (buffers)
            if (typeof(T) == typeof(byte))
            {
                return(Convert.FromBase64String(valueString));
            }
            string[] valueParts = valueString.Split(NameConst.sepElem);
            var      array      = new T[valueParts.Length];

            for (int i = 0; i < valueParts.Length; i++)
            {
                array[i] = (T)DeserialiseScalar(NamedValue.DecodeText(valueParts[i]));
            }
            return(array);
        }
Esempio n. 14
0
 public NamedValueSet(object[,] properties)
 {
     if (properties != null)
     {
         _dict.Locked(dict =>
         {
             for (int i = 0; i < properties.GetLength(0); i++)
             {
                 if (properties[i, 0] != null)
                 {
                     var nv = new NamedValue(properties[i, 0].ToString(), properties[i, 1]);
                     dict[nv.Name.ToLowerInvariant()] = nv;
                 }
             }
         });
     }
 }
Esempio n. 15
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public string SerialiseVector(object value)
        {
            // special case
            if (value is byte[] bytes)
            {
                return(Convert.ToBase64String(bytes));
            }
            var array = (T[])value;
            var sb    = new StringBuilder();

            for (int i = 0; i < array.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(NameConst.sepElem);
                }
                sb.Append(NamedValue.EncodeText(SerialiseScalar(array[i])));
            }
            return(sb.ToString());
        }
Esempio n. 16
0
 private void Deserialise(string text)
 {
     // construct from serialised text
     if (text != null)
     {
         string   delims    = NameConst.sepList + Environment.NewLine;
         string[] textParts = text.Split(delims.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
         _dict.Locked(dict =>
         {
             foreach (string textPart in textParts)
             {
                 if (!string.IsNullOrEmpty(textPart.Trim()))
                 {
                     var nv = new NamedValue(textPart);
                     dict[nv.Name.ToLowerInvariant()] = nv;
                 }
             }
         });
     }
 }
Esempio n. 17
0
        /// <summary>
        /// Sets the specified new named value.
        /// </summary>
        /// <param name="name">The new named value.</param>
        /// <param name="value">The value </param>
        /// <returns></returns>
        public void Set(string name, object value)
        {
            if (_frozen)
            {
                throw new InvalidOperationException("Cannot modify frozen object");
            }
            string key = name.ToLowerInvariant();

            if (value != null)
            {
                _dict.Locked(dict =>
                {
                    var nv    = new NamedValue(name, value);
                    dict[key] = nv;
                });
            }
            else
            {
                // null - remove named value
                _dict.Locked(dict => { dict.Remove(key); });
            }
        }
Esempio n. 18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NamedValueSet"/> class.
 /// </summary>
 /// <param name="names">The names array.</param>
 /// <param name="values">The values array.</param>
 public NamedValueSet(string[] names, object[] values)
 {
     if (names == null)
     {
         throw new ArgumentNullException(nameof(names));
     }
     if (values == null)
     {
         throw new ArgumentNullException(nameof(values));
     }
     if (names.Length != values.Length)
     {
         throw new ArgumentException("names.Length != values.Length");
     }
     _dict.Locked(dict =>
     {
         for (int i = 0; i < names.Length; i++)
         {
             var nv = new NamedValue(names[i], values[i]);
             dict[nv.Name.ToLowerInvariant()] = nv;
         }
     });
 }
Esempio n. 19
0
        /// <summary>
        /// Finds the array value with the given name, or returns an empty array.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name">The name of the array.</param>
        /// <returns></returns>
        public T[] GetArray <T>(string name)
        {
            NamedValue nv = Get(name, false);

            return(nv?.AsArray <T>());
        }