@@ -62,42 +62,46 @@ public bool IsService(Type serviceType)
6262 return _services . Any ( sd => IsServiceType ( sd . ServiceType , serviceType ) ) ;
6363 }
6464
65- private object [ ] GetServices ( Type serviceType )
65+ private object CreateService ( ServiceDescriptor serviceDescriptor , Type requestedType )
6666 {
67+ object ? instance = serviceDescriptor . ImplementationInstance ;
68+ if ( instance == null && serviceDescriptor . ImplementationFactory != null )
69+ {
70+ instance = serviceDescriptor . ImplementationFactory ( this ) ;
71+ }
72+
73+ if ( instance == null && serviceDescriptor . ImplementationType != null )
74+ {
75+ Type implementationType = serviceDescriptor . ImplementationType ! ;
76+ if ( implementationType . IsGenericTypeDefinition )
77+ implementationType = MakeGenericType ( implementationType , requestedType . GenericTypeArguments ) ;
78+
79+ instance = ActivatorUtilities . CreateInstance ( this , implementationType ) ;
80+ }
81+
82+ return instance ! ;
83+
6784 [ UnconditionalSuppressMessage (
6885 "AotAnalysis" ,
6986 "IL3050:RequiresDynamicCode" ,
70- Justification = "VerifyAotCompatibility ensures that dynamic code supported" ) ]
87+ Justification = "VerifyAotCompatibility ensures that dynamic code is supported" ) ]
88+ [ return : DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicConstructors ) ]
7189 static Type MakeGenericType ( Type type , params Type [ ] typeArguments )
7290 {
7391 if ( VerifyAotCompatibility )
74- throw new InvalidOperationException ( "Cannot build generic type when using AOT." ) ;
75-
76- return type . MakeGenericType ( typeArguments ) ;
77- }
78-
79- object ServiceFactory ( ServiceDescriptor serviceDescriptor )
80- {
81- object ? instance = serviceDescriptor . ImplementationInstance ;
82- if ( instance == null && serviceDescriptor . ImplementationFactory != null )
8392 {
84- instance = serviceDescriptor . ImplementationFactory ( this ) ;
93+ throw new InvalidOperationException (
94+ $ "Cannot build generic type '{ type . GetDisplayName ( ) } ' when using AOT") ;
8595 }
8696
87- if ( instance == null && serviceDescriptor . ImplementationType != null )
88- {
89- Type implementationType = serviceDescriptor . ImplementationType ! ;
90- if ( implementationType . IsGenericTypeDefinition )
91- implementationType = MakeGenericType ( implementationType , serviceType . GenericTypeArguments ) ;
92-
93- instance = ActivatorUtilities . CreateInstance ( this , implementationType ) ;
94- }
95-
96- return instance ! ;
97+ return type . MakeGenericType ( typeArguments ) ;
9798 }
99+ }
98100
101+ private object [ ] GetServices ( Type serviceType )
102+ {
99103 return _services . Where ( sd => IsServiceType ( sd . ServiceType , serviceType ) )
100- . Select ( ServiceFactory )
104+ . Select ( sd => CreateService ( sd , serviceType ) )
101105 . ToArray ( ) ;
102106 }
103107
@@ -139,7 +143,7 @@ object ServiceFactory(ServiceDescriptor serviceDescriptor)
139143 static Array CreateArray ( Type elementType , int length )
140144 {
141145 if ( VerifyAotCompatibility && elementType . IsValueType )
142- throw new InvalidOperationException ( "Cannot build array of value service types. " ) ;
146+ throw new InvalidOperationException ( "Cannot build array of value service types when using AOT " ) ;
143147
144148 return Array . CreateInstance ( elementType , length ) ;
145149 }
0 commit comments