@@ -170,6 +170,11 @@ public static IServiceCollection Decorate(this IServiceCollection services, Type
170170 Preconditions . NotNull ( serviceType , nameof ( serviceType ) ) ;
171171 Preconditions . NotNull ( decorator , nameof ( decorator ) ) ;
172172
173+ if ( serviceType . IsOpenGeneric ( ) )
174+ {
175+ return services . DecorateOpenGeneric ( serviceType , decorator ) ;
176+ }
177+
173178 return services . DecorateDescriptors ( serviceType , x => x . Decorate ( decorator ) ) ;
174179 }
175180
@@ -243,10 +248,28 @@ private static bool IsSameGenericType(Type t1, Type t2)
243248 return t1 . IsGenericType && t2 . IsGenericType && t1 . GetGenericTypeDefinition ( ) == t2 . GetGenericTypeDefinition ( ) ;
244249 }
245250
246- private static bool TryDecorateOpenGeneric ( this IServiceCollection services , Type serviceType , Type decoratorType )
251+ private static IServiceCollection DecorateOpenGeneric ( this IServiceCollection services , Type serviceType , Func < object , IServiceProvider , object > decorator )
247252 {
248253 bool TryDecorate ( Type [ ] typeArguments )
249254 {
255+ var closedServiceType = serviceType . MakeGenericType ( typeArguments ) ;
256+ return services . TryDecorateDescriptors ( closedServiceType , x => x . Decorate ( decorator ) ) ;
257+ }
258+
259+ if ( services . TryDecorateOpenGeneric ( serviceType , openTypeDecorator : TryDecorate ) )
260+ {
261+ return services ;
262+ }
263+
264+ throw new MissingTypeRegistrationException ( serviceType ) ;
265+ }
266+
267+ private static bool TryDecorateOpenGeneric ( this IServiceCollection services , Type serviceType , Type decoratorType = null , Func < Type [ ] , bool > openTypeDecorator = null )
268+ {
269+ bool TryDecorate ( Type [ ] typeArguments )
270+ {
271+ Preconditions . NotNull ( decoratorType , nameof ( decoratorType ) ) ;
272+
250273 var closedServiceType = serviceType . MakeGenericType ( typeArguments ) ;
251274 var closedDecoratorType = decoratorType . MakeGenericType ( typeArguments ) ;
252275
@@ -263,7 +286,8 @@ bool TryDecorate(Type[] typeArguments)
263286 return false ;
264287 }
265288
266- return arguments . Aggregate ( true , ( result , args ) => result && TryDecorate ( args ) ) ;
289+ var tryDecorate = openTypeDecorator ?? TryDecorate ;
290+ return arguments . Aggregate ( true , ( result , args ) => result && tryDecorate ( args ) ) ;
267291 }
268292
269293 private static IServiceCollection DecorateDescriptors ( this IServiceCollection services , Type serviceType , Func < ServiceDescriptor , ServiceDescriptor > decorator )
0 commit comments