Beispiel #1
0
        /// <summary>
        /// Adds a value to the <see cref="INamedList{T}"/> of key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="name">[optional]
        /// The name of the new <see cref="INamedList{T}"/>.
        /// </param>
        /// <remarks>
        /// If the key already exists the name isn't needed; if the key doesn't
        /// exists a name is needed, otherwise a
        /// <see cref="KeyNotFoundException"/> will be thrown.
        /// </remarks>
        /// <exception cref="KeyNotFoundException">
        /// If the key isn't there AND a name for the new
        /// <see cref="INamedList{T}"/> was missing.
        /// </exception>
        public void Add(K key, V value, string name = null)
        {
            if (!ContainsKey(key))
            {
                if (string.IsNullOrEmpty(name))
                {
                    KeyNotFoundException k =
                        new($"Key '{key}' is missing AND name" +
                            $"for creating a new INamedList<{typeof(V)}> is null or empty as well");
                    throw k;
                }
                else
                {
                    Add(key, new NamedList <V>(name));
                    EventArgs <INamedList <V> > eventArgs = new(new NamedList <V>(name));
                    OnNamedListAdded?.Invoke(this, eventArgs);
                }
            }

            this[key].Add(value);
        }
Beispiel #2
0
 /// <summary>
 /// Adds a key.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="name">The name of the new <see cref="INamedList{T}"/>.</param>
 /// <exception cref="ArgumentException">If key already exists.</exception>
 public void Add(K key, string name)
 {
     if (typeof(V).Equals(typeof(string)))
     {
         V tmpV = (V)Convert.ChangeType(name, typeof(V));
         Add(key, tmpV, null);
     }
     else
     {
         if (!ContainsKey(key))
         {
             Add(key, new NamedList <V>(name));
             EventArgs <INamedList <V> > eventArgs = new(new NamedList <V>(name));
             OnNamedListAdded?.Invoke(this, eventArgs);
         }
         else
         {
             ArgumentException ae = new(
                 $"Key '{key}' already exists.",
                 "key");
             throw ae;
         }
     }
 }