예제 #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);
            }
        }
예제 #2
0
        /// <summary>
        /// Adds a value to the dictionary.
        /// </summary>
        /// <param name="key">The key type.</param>
        /// <param name="value">The value.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="key"/> is <c>null</c>.</exception>
        public void Add(Type key, V value)
        {
            key.ThrowIfNull(nameof(key));

            // Add a regular class entry
            try
            {
                _dictionary.Add(key, value);
            }
            catch (ArgumentException)
            {
                // This exception is thrown if the dictonary already contains the key
                // Just make it bit more verbose.
                throw new ArgumentException(Properties.Resources.TypeAlreadyExists.FormatString(key));
            }

            // Make references for the interfaces as well
            foreach (var ctype in InterfaceCache.Get(key))
            {
                if (!_interfaces.TryGetValue(ctype, out var values))
                {
                    values = new TypeValues <V>();
                    _interfaces.Add(ctype, values);
                }
                values.Add(value);
            }
        }