/// <summary> /// Registers the instance in the <paramref name="container"/> as the instance that will be /// provided when <see cref="CommonProvider.Provide{TRequested}" /> or /// <see cref="CommonProvider.ProvideType" /> are called on the container for type of the /// instance. /// </summary> /// <exception cref="ArgumentNullException"> /// Thrown when one or more required arguments are null. /// </exception> /// <param name="instance"> The instance. </param> /// <param name="container"> The container. </param> public static void RegisterAsProvidedInstance( [NotNull] this object instance, [NotNull] IObjectContainer container) { //- Validate if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (container == null) { throw new ArgumentNullException(nameof(container)); } // Register container.RegisterProvidedInstance(instance.GetType(), instance); }
/// <summary> /// Registers the <paramref name="instance"/> in the <paramref name="container"/> as the /// instance that will be provided when /// <see cref="CommonProvider.Provide{TRequested}" /> or /// <see cref="CommonProvider.ProvideType" /> is called on the container for type /// <paramref name="type" /> /// </summary> /// <exception cref="ArgumentNullException"> /// Thrown when one or more required arguments are null. /// </exception> /// <param name="instance"> The instance. </param> /// <param name="type"> The type that will be requested. </param> /// <param name="container"> The container. </param> public static void RegisterAsProvidedInstance( [NotNull] this object instance, [NotNull] Type type, [CanBeNull] IObjectContainer container) { //- Validate if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } var hasContainer = instance as IHasContainer <IObjectContainer>; if (container == null) { if (hasContainer != null) { container = hasContainer.Container; } else { throw new ArgumentNullException(nameof(container)); } } // Safeguard against sticking items in a different container if (hasContainer != null && hasContainer.Container != container) { throw new InvalidOperationException("The instance already has a container different from this container"); } // Register container.RegisterProvidedInstance(type, instance); }