Exemple #1
0
        /// <inheritdoc/>
        public void Add(Type key, V value)
        {
            key.ThrowIfNull(nameof(key));

            // We should always be able to access the type by itself, so remove any ambiguous elements if necessary
            if (_dictionary.TryGetValue(key, out var values))
            {
                if (values.IsDirect)
                {
                    throw new ArgumentException(Properties.Resources.TypeAlreadyExists.FormatString(key.FullName));
                }
            }
            else
            {
                values = new TypeValues <V>();
                _dictionary.Add(key, values);
            }
            values.Add(value, true);
            _values.Add(value);

            foreach (var type in InheritanceCache.Get(key).Union(InterfaceCache.Get(key)))
            {
                if (!_dictionary.TryGetValue(type, out values))
                {
                    values = new TypeValues <V>();
                    _dictionary.Add(type, values);
                }
                values.Add(value);
            }
        }
Exemple #2
0
 /// <inheritdoc/>
 public bool Remove(Type key, V value)
 {
     key.ThrowIfNull(nameof(key));
     if (_dictionary.TryGetValue(key, out var values))
     {
         if (!values.IsDirect || !values.Value.Equals(value))
         {
             return(false);
         }
         _values.Remove(value);
         foreach (var type in InheritanceCache.Get(key).Union(InterfaceCache.Get(key)))
         {
             _dictionary[type].Remove(value);
         }
         _dictionary.Remove(key);
         return(true);
     }
     return(false);
 }