public static void AddClassesWithServiceAttribute()

in generators/app/templates/default/src/Foundation/DependencyInjection/CodeFolderX/ServiceCollectionExtensions.cs [28:54]


        public static void AddClassesWithServiceAttribute(this IServiceCollection serviceCollection, params Assembly[] assemblies)
        {
            var typesWithAttributes = assemblies
                .Where(assembly => !assembly.IsDynamic)
                .SelectMany(GetExportedTypes)
                .Where(type => !type.IsAbstract && !type.IsGenericTypeDefinition)
                .Select(type =>
                    new
                    {
                        Lifetime = type.GetCustomAttribute<ServiceAttribute>()?.Lifetime,
                        ServiceType = type,
                        ImplementationType = type.GetCustomAttribute<ServiceAttribute>()?.ServiceType
                    })
                .Where(t => t.Lifetime != null);

            foreach (var type in typesWithAttributes)
            {
                if (type.ImplementationType == null)
                {
                    serviceCollection.Add(type.ServiceType, type.Lifetime.Value);
                }
                else
                {
                    serviceCollection.Add(type.ImplementationType, type.ServiceType, type.Lifetime.Value);
                }
            }
        }