/// <summary> /// Register a type mapping with the container. /// </summary> /// <typeparam name="T">Type of instance to register.</typeparam> /// <param name="instance">Object to return.</param> /// <param name="label">A unique label that allows multiple implementations of the same type.</param> /// <returns>The current IocContainer instance.</returns> public IocContainer Register <T>(T instance, string label = null) { this.CheckDisposed(); if (instance == null) { throw new ArgumentNullException("instance"); } if (label == null) { label = DefaultLabel; } Type instanceType = typeof(T); this._readerWriterLock.AcquireWriterLock(Timeout.Infinite); try { if (this._container.ContainsKey(instanceType) && this._container[instanceType] != null && this._container[instanceType].ContainsKey(label) && this._container[instanceType][label] != null) { if (this._container[instanceType][label].HasInstance) { throw new InvalidOperationException(string.Format("An instance of type {0} for label {1} already exists. Unregister the instance first.", instanceType.FullName, label)); } else { this._container[instanceType][label].Instance = instance; return(this); } } else { if (!this._container.ContainsKey(instanceType) || this._container[instanceType] == null) { this._container.Add(instanceType, new Dictionary <string, IocRegistration>(this._ignoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.CurrentCulture)); } if (!this._container[instanceType].ContainsKey(label) || this._container[instanceType][label] == null) { this._container[instanceType].Add(label, new IocRegistration()); } this._container[instanceType][label].Instance = instance; return(this); } } catch (Exception e) { InternalLogger.Log(e); throw; } finally { this._readerWriterLock.ReleaseWriterLock(); } }
/// <summary> /// Try to resolve an instance of the requested type from the container. /// </summary> /// <typeparam name="T">Type to resolve.</typeparam> /// <param name="instance">The retrieved object, if it is possible to resolve one.</param> /// <param name="createNew">true to call delegate method to create a new instance; false to return a shared instance.</param> /// <param name="label">A unique label that allows multiple implementations of the same type.</param> /// <returns>true if resolve successfully; otherwise, false.</returns> public bool TryResolve <T>(out T instance, bool createNew = false, string label = null) { this.CheckDisposed(); if (label == null) { label = DefaultLabel; } Type instanceType = typeof(T); this._readerWriterLock.AcquireReaderLock(Timeout.Infinite); try { if (!this._container.ContainsKey(instanceType) || this._container[instanceType] == null || !this._container[instanceType].ContainsKey(label) || this._container[instanceType][label] == null) { instance = default(T); return(false); } else { if (createNew) { if (this._container[instanceType][label].HasCreation) { instance = (T)this._container[instanceType][label].Creation.Invoke(); return(true); } else { instance = default(T); return(false); } } else { if (this._container[instanceType][label].HasInstance) { instance = (T)this._container[instanceType][label].Instance; return(true); } else { instance = default(T); return(false); } } } } catch (Exception e) { InternalLogger.Log(e); instance = default(T); return(false); } finally { this._readerWriterLock.ReleaseReaderLock(); } }