using Microsoft.Extensions.DependencyInjection;
public static class Program
public static void Main()
Console.WriteLine("Hello World");
public static void AddDecorator<TInterface, TDecorator>( this IServiceCollection services )
where TDecorator : class, TInterface
var wrappedDescriptor = services.FirstOrDefault(
s => s.ServiceType == typeof( TInterface ) );
if ( wrappedDescriptor == null )
throw new InvalidOperationException( $"{typeof( TInterface ).Name} is not registered" );
var objectFactory = ActivatorUtilities.CreateFactory(
new[] { typeof( TInterface ) } );
services.Replace( ServiceDescriptor.Describe(
s => (TInterface) objectFactory( s, new[] { s.CreateInstance( wrappedDescriptor ) } ),
wrappedDescriptor.Lifetime )