/// <summary> /// Gets or create the requested instance specified by the parameter <typeparamref name="T"/>. /// </summary> /// <remarks>This</remarks> /// <typeparam name="T">The type of the instance that is requested. /// </typeparam> /// <returns>If the type specified by <typeparamref name="T"/> is /// registered, it returns an instance that is, is a super class of, or /// implements the type specified by <typeparamref name="T"/>. Otherwise /// a <see cref="InstanceNotFoundInEnvironmentConfigurationException"/> /// occurs. /// </returns> public static T Get <T>() where T : class { Contract.Ensures(Contract.Result <T>() != null, "The result cannot be null."); Log.DebugFormat("Requesting instance {0} from the environment.", typeof(T).FullName); T result = null; if (_instance == null || !_instance.TryGet(out result)) { object defaultResult; if (_defaults.TryGetValue(typeof(T), out defaultResult)) { result = (T)defaultResult; } } if (result == null) { throw new InstanceNotFoundInEnvironmentConfigurationException(typeof(T)); } return(result); }
public bool TryGet <T>(out T result) where T : class { result = null; var type = typeof(T); Func <object> factory; if (_factories.TryGetValue(type, out factory)) { result = (T)factory(); return(true); } return(_configuration != null && _configuration.TryGet(out result)); }
/// <summary> /// Gets or create the requested instance specified by the parameter <typeparamref name="T"/>. /// </summary> /// <remarks>This</remarks> /// <typeparam name="T">The type of the instance that is requested. /// </typeparam> /// <returns>If the type specified by <typeparamref name="T"/> is /// registered, it returns an instance that is, is a super class of, or /// implements the type specified by <typeparamref name="T"/>. Otherwise /// a <see cref="InstanceNotFoundInEnvironmentConfigurationException"/> /// occurs. /// </returns> public static T Get <T>() where T : class { //Contract.Ensures(Contract.Result<T>() != null, "The result cannot be null."); T result = null; if (_instance == null || !_instance.TryGet(out result)) { object defaultResult; if (_defaults.TryGetValue(typeof(T), out defaultResult)) { result = (T)defaultResult; } } if (result == null) { throw new InstanceNotFoundInEnvironmentConfigurationException(typeof(T)); } return(result); }