private ConstructorInfo GetTargetConstructor(IComponentContext context, ConcreteReflectionActivatorData data, IEnumerable <Parameter> parameters) { var constructors = data.ConstructorFinder.FindConstructors(data.ImplementationType); var constructorBindings = GetConstructorBindings(_componentContext, parameters, constructors); var validBindings = constructorBindings.Where(cb => cb.CanInstantiate).ToArray(); if (validBindings.Length == 0) { throw new DependencyResolutionException(GetBindingFailureMessage(constructorBindings, data)); } return(data.ConstructorSelector.SelectConstructorBinding(validBindings).TargetConstructor); }
public IEnumerable <IComponentRegistration> RegistrationsFor(Service service, Func <Service, IEnumerable <IComponentRegistration> > registrationAccessor) { if (!(service is IServiceWithType swt)) { return(Enumerable.Empty <IComponentRegistration>()); } var reg = _registrations.FirstOrDefault(x => x.Type == swt.ServiceType); if (reg == null) { return(Enumerable.Empty <IComponentRegistration>()); } var activatorRegistration = reg as IActivatorContainerRegistration; if (activatorRegistration != null) { var registration = new ComponentRegistration( Guid.NewGuid(), new DelegateActivator(swt.ServiceType, (c, p) => { var result = activatorRegistration.Activator(_resolver.WithContext(c)); return(result); }), reg.Lifetime == Lifetime.Request ? PerRequestLifetime() : reg.Lifetime == Lifetime.Singleton ? (IComponentLifetime) new RootScopeLifetime() : new CurrentScopeLifetime(), reg.Lifetime == Lifetime.Request || reg.Lifetime == Lifetime.Singleton ? InstanceSharing.Shared : InstanceSharing.None, reg.Lifetime == Lifetime.ExternallyOwned ? InstanceOwnership.ExternallyOwned : InstanceOwnership.OwnedByLifetimeScope, new[] { service }, new Dictionary <string, object>()); return(new IComponentRegistration[] { registration }); } else { var activatorData = new ConcreteReflectionActivatorData(reg.Type); var registration = new ComponentRegistration( Guid.NewGuid(), activatorData.Activator, reg.Lifetime == Lifetime.Request ? PerRequestLifetime() : reg.Lifetime == Lifetime.Singleton ? (IComponentLifetime) new RootScopeLifetime() : new CurrentScopeLifetime(), reg.Lifetime == Lifetime.Request || reg.Lifetime == Lifetime.Singleton ? InstanceSharing.Shared : InstanceSharing.None, reg.Lifetime == Lifetime.ExternallyOwned ? InstanceOwnership.ExternallyOwned : InstanceOwnership.OwnedByLifetimeScope, new[] { service }, new Dictionary <string, object>()); return(new IComponentRegistration[] { registration }); } }
private string GetBindingFailureMessage(IEnumerable <ConstructorParameterBinding> constructorBindings, ConcreteReflectionActivatorData data) { var reasons = new StringBuilder(); foreach (var invalid in constructorBindings.Where(cb => !cb.CanInstantiate)) { reasons.AppendLine(); reasons.Append(invalid.Description); } return(string.Format("NoConstructorsBindable", data.ConstructorFinder, data.ImplementationType, reasons)); }
internal static void MapActivatorData(Type serviceType, ConcreteReflectionActivatorData concreteReflectionActivatorData) { ActivatorCache.TryAdd(serviceType, concreteReflectionActivatorData); }