/// <summary> /// Registers a concrete object instance to the given provider /// </summary> /// <param name="y">The type to register</param> /// <param name="o">The object instance to register</param> /// <param name="lifetimeManager">The type of the ServiceProvider to use for resolution</param> public static void RegisterInstance(Type y, object o, Type lifetimeManager = null) { if (y is null) { throw new ArgumentNullException(nameof(y)); } if (o is null) { throw new ArgumentNullException(nameof(o)); } if (!y.IsAssignableFrom(o.GetType())) { throw new ArgumentException($"Can not register object of type {o.GetType().FullName} to type {y.FullName} because it is not assignable"); } lifetimeManager = lifetimeManager ?? typeof(SingletonServiceProvider); Registration thisRegistration = new Registration() { ServiceProvider = lifetimeManager, RegisteredType = y }; AbstractServiceProvider instance = Activator.CreateInstance(lifetimeManager) as AbstractServiceProvider; instance.Add(y, o); AddRegistration(thisRegistration); }
/// <summary> /// Registers a service provider instance to be used for object resolution /// </summary> /// <param name="serviceProvider">The service provider to register</param> public void Register(AbstractServiceProvider serviceProvider) { if (serviceProvider is StaticServiceProvider) { StaticProviders.Add(serviceProvider.GetType(), serviceProvider as StaticServiceProvider); } else if (serviceProvider is ScopedServiceProvider) { this.ScopedProviders.Add(serviceProvider.GetType(), serviceProvider as ScopedServiceProvider); } else { throw new ArgumentException(WRONG_SERVICE_PROVIDER_MESSAGE); } this.AllProviders.Add(serviceProvider.GetType(), serviceProvider); }
public static new bool IsTypeProvideService(Type type, Type serviceTypeOrOpenGeneric) { return(AbstractServiceProvider.IsTypeProvideService(type, serviceTypeOrOpenGeneric)); }