Пример #1
0
        /// <summary>
        ///     Check if a particular type/instance name has been registered with the container
        /// </summary>
        /// <param name="type">Type to check registration for</param>
        /// <param name="instanceName">Instance name (optional)</param>
        /// <returns>
        ///     <c>true</c>if the type/instance name has been registered
        ///     with the container; otherwise <c>false</c>
        /// </returns>
        /// <exception cref="ArgumentNullException" />
        public bool IsRegistered(Type type, string instanceName = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var key = new MappingKey(type, instanceName);

            return(this._mappings.ContainsKey(key));
        }
Пример #2
0
        /// <summary>
        ///     Resolve an instance of the requested type from the container.
        /// </summary>
        /// <param name="type">Requested type</param>
        /// <param name="parameters">Constructor parameters</param>
        /// <param name="instanceName">Instance name (optional)</param>
        /// <returns>The retrieved object</returns>
        /// <exception cref="InvalidOperationException" />
        public object Create(Type type, dynamic parameters, string instanceName = null)
        {
            var key = new MappingKey(type, instanceName);
            Func <dynamic, object> createInstance;

            if (!this._mappings.TryGetValue(key, out createInstance))
            {
                throw new InvalidOperationException($"Could not find mapping for type '{type.FullName}'");
            }

            return(createInstance(parameters));
        }
Пример #3
0
        /// <summary>
        ///     Register a type mapping
        /// </summary>
        /// <param name="type">Type that will be requested</param>
        /// <param name="createInstanceDelegate">
        ///     A delegate that will be used to
        ///     create an instance of the requested object
        /// </param>
        /// <param name="instanceName">Instance name (optional)</param>
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="InvalidOperationException" />
        public void Register(Type type, Func <dynamic, object> createInstanceDelegate, string instanceName = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (createInstanceDelegate == null)
            {
                throw new ArgumentNullException(nameof(createInstanceDelegate));
            }

            var key = new MappingKey(type, instanceName);

            this._mappings.TryAdd(key, createInstanceDelegate);
        }
Пример #4
0
        /// <summary>
        ///     Unregister a type mapping
        /// </summary>
        /// <param name="from">Type that will be unregistered</param>
        /// <param name="instanceName">
        ///     Instance name (optional).
        /// </param>
        /// <exception cref="ArgumentNullException" />
        public void Unregister(Type from, string instanceName = null)
        {
            if (from == null)
            {
                throw new ArgumentNullException(nameof(@from));
            }

            var key = new MappingKey(from, instanceName);

            if (!this._mappings.ContainsKey(key))
            {
                return;
            }
            Func <dynamic, object> outValue;

            this._mappings.TryRemove(key, out outValue);
        }