public void Execute(IReadOnlyCollection <Type> types, IServiceCollection services) { foreach (var type in types) { if (!IsConcreteClassOf(type, _type, out var baseTypes)) { continue; } var lifetime = ServiceLifetimeProvider.GetLifetimeOrNullFromAttribute(type) ?? _lifetime; foreach (var baseType in baseTypes) { services.Add(new ServiceDescriptor(baseType, type, lifetime)); } } }
public void Execute(IReadOnlyCollection <Type> types, IServiceCollection services) { var interfaces = types.Where(x => x.IsInterface); var classes = types.Where(x => x.IsConcreteClass()).ToList(); foreach (var @interface in interfaces) { var @class = classes.FirstOrDefault(c => @interface.Namespace == c.Namespace && @interface.Name.Substring(1) == c.Name); if (@class == null) { continue; } if ([email protected](@class)) { continue; } var lifetime = ServiceLifetimeProvider.GetLifetimeOrNullFromAttribute(@class) ?? _serviceLifetime; services.Add(new ServiceDescriptor(@interface, @class, lifetime)); } }
public void ShouldGetLifetime() { ServiceLifetimeProvider.GetLifetimeOrNullFromAttribute(typeof(Type1)).ShouldBeNull(); ServiceLifetimeProvider.GetLifetimeOrNullFromAttribute(typeof(Type2)).ShouldBe(ServiceLifetime.Singleton); }