@@ -14,6 +14,7 @@ sealed class DynamicallyAccessedMembersTypeHierarchy
14
14
{
15
15
readonly LinkContext _context ;
16
16
readonly MarkStep _markStep ;
17
+ readonly ReflectionMarker _reflectionMarker ;
17
18
18
19
// Cache of DynamicallyAccessedMembers annotations applied to types and their hierarchies
19
20
// Values
@@ -43,6 +44,7 @@ public DynamicallyAccessedMembersTypeHierarchy (LinkContext context, MarkStep ma
43
44
_context = context ;
44
45
_markStep = markStep ;
45
46
_typesInDynamicallyAccessedMembersHierarchy = new Dictionary < TypeDefinition , ( DynamicallyAccessedMemberTypes , bool ) > ( ) ;
47
+ _reflectionMarker = new ReflectionMarker ( _context , _markStep , enabled : true ) ;
46
48
}
47
49
48
50
public ( DynamicallyAccessedMemberTypes annotation , bool applied ) ProcessMarkedTypeForDynamicallyAccessedMembersHierarchy ( TypeDefinition type )
@@ -107,10 +109,8 @@ public DynamicallyAccessedMembersTypeHierarchy (LinkContext context, MarkStep ma
107
109
if ( apply ) {
108
110
// One of the base/interface types is already marked as having the annotation applied
109
111
// so we need to apply the annotation to this type as well
110
- var origin = new MessageOrigin ( type ) ;
111
- var reflectionMarker = new ReflectionMarker ( _context , _markStep , enabled : true ) ;
112
112
// Report warnings on access to annotated members, with the annotated type as the origin.
113
- ApplyDynamicallyAccessedMembersToType ( reflectionMarker , origin , type , annotation ) ;
113
+ ApplyDynamicallyAccessedMembersToType ( type , annotation ) ;
114
114
}
115
115
116
116
return ( annotation , apply ) ;
@@ -126,10 +126,8 @@ public DynamicallyAccessedMemberTypes ApplyDynamicallyAccessedMembersToTypeHiera
126
126
return annotation ;
127
127
128
128
// Apply the effective annotation for the type
129
- var origin = new MessageOrigin ( type ) ;
130
- var reflectionMarker = new ReflectionMarker ( _context , _markStep , enabled : true ) ;
131
129
// Report warnings on access to annotated members, with the annotated type as the origin.
132
- ApplyDynamicallyAccessedMembersToType ( reflectionMarker , origin , type , annotation ) ;
130
+ ApplyDynamicallyAccessedMembersToType ( type , annotation ) ;
133
131
134
132
// Mark it as applied in the cache
135
133
_typesInDynamicallyAccessedMembersHierarchy [ type ] = ( annotation , true ) ;
@@ -161,16 +159,14 @@ public DynamicallyAccessedMemberTypes ApplyDynamicallyAccessedMembersToTypeHiera
161
159
break ;
162
160
163
161
foreach ( var candidateType in candidateTypes ) {
164
- ApplyDynamicallyAccessedMembersToTypeHierarchyInner ( reflectionMarker , candidateType ) ;
162
+ ApplyDynamicallyAccessedMembersToTypeHierarchyInner ( candidateType ) ;
165
163
}
166
164
}
167
165
168
166
return annotation ;
169
167
}
170
168
171
- bool ApplyDynamicallyAccessedMembersToTypeHierarchyInner (
172
- in ReflectionMarker reflectionMarker ,
173
- TypeDefinition type )
169
+ bool ApplyDynamicallyAccessedMembersToTypeHierarchyInner ( TypeDefinition type )
174
170
{
175
171
( var annotation , var applied ) = GetCachedInfoForTypeInHierarchy ( type ) ;
176
172
@@ -182,13 +178,13 @@ bool ApplyDynamicallyAccessedMembersToTypeHierarchyInner (
182
178
183
179
TypeDefinition ? baseType = _context . TryResolve ( type . BaseType ) ;
184
180
if ( baseType != null )
185
- applied = ApplyDynamicallyAccessedMembersToTypeHierarchyInner ( reflectionMarker , baseType ) ;
181
+ applied = ApplyDynamicallyAccessedMembersToTypeHierarchyInner ( baseType ) ;
186
182
187
183
if ( ! applied && type . HasInterfaces ) {
188
184
foreach ( InterfaceImplementation iface in type . Interfaces ) {
189
185
var interfaceType = _context . TryResolve ( iface . InterfaceType ) ;
190
186
if ( interfaceType != null ) {
191
- if ( ApplyDynamicallyAccessedMembersToTypeHierarchyInner ( reflectionMarker , interfaceType ) ) {
187
+ if ( ApplyDynamicallyAccessedMembersToTypeHierarchyInner ( interfaceType ) ) {
192
188
applied = true ;
193
189
break ;
194
190
}
@@ -197,31 +193,33 @@ bool ApplyDynamicallyAccessedMembersToTypeHierarchyInner (
197
193
}
198
194
199
195
if ( applied ) {
200
- var origin = new MessageOrigin ( type ) ;
201
196
// Report warnings on access to annotated members, with the annotated type as the origin.
202
- ApplyDynamicallyAccessedMembersToType ( reflectionMarker , origin , type , annotation ) ;
197
+ ApplyDynamicallyAccessedMembersToType ( type , annotation ) ;
203
198
_typesInDynamicallyAccessedMembersHierarchy [ type ] = ( annotation , true ) ;
204
199
}
205
200
206
201
return applied ;
207
202
}
208
203
209
- void ApplyDynamicallyAccessedMembersToType ( in ReflectionMarker reflectionMarker , in MessageOrigin origin , TypeDefinition type , DynamicallyAccessedMemberTypes annotation )
204
+ void ApplyDynamicallyAccessedMembersToType ( TypeDefinition type , DynamicallyAccessedMemberTypes annotation )
210
205
{
206
+ var origin = new MessageOrigin ( type ) ;
211
207
Debug . Assert ( annotation != DynamicallyAccessedMemberTypes . None ) ;
212
208
213
209
// We need to apply annotations to this type, and its base/interface types (recursively)
214
- // But the annotations on base/interfaces are already applied so we don't need to apply those
210
+ // But the annotations on base will be applied so we don't need to apply those
215
211
// again (and should avoid doing so as it would produce extra warnings).
216
212
var baseType = _context . TryResolve ( type . BaseType ) ;
217
213
if ( baseType != null ) {
218
214
var baseAnnotation = GetCachedInfoForTypeInHierarchy ( baseType ) ;
219
- var annotationToApplyToBase = baseAnnotation . applied ? Annotations . GetMissingMemberTypes ( annotation , baseAnnotation . annotation ) : annotation ;
215
+ if ( ! baseAnnotation . applied && baseAnnotation . annotation != DynamicallyAccessedMemberTypes . None )
216
+ ApplyDynamicallyAccessedMembersToType ( baseType , baseAnnotation . annotation ) ;
217
+ var annotationToApplyToBase = Annotations . GetMissingMemberTypes ( annotation , baseAnnotation . annotation ) ;
220
218
221
219
// Apply any annotations that didn't exist on the base type to the base type.
222
220
// This may produce redundant warnings when the annotation is DAMT.All or DAMT.PublicConstructors and the base already has a
223
221
// subset of those annotations.
224
- reflectionMarker . MarkTypeForDynamicallyAccessedMembers ( origin , baseType , annotationToApplyToBase , DependencyKind . DynamicallyAccessedMemberOnType , declaredOnly : false ) ;
222
+ _reflectionMarker . MarkTypeForDynamicallyAccessedMembers ( origin , baseType , annotationToApplyToBase , DependencyKind . DynamicallyAccessedMemberOnType , declaredOnly : false ) ;
225
223
}
226
224
227
225
// Most of the DynamicallyAccessedMemberTypes don't select members on interfaces. We only need to apply
@@ -234,19 +232,21 @@ void ApplyDynamicallyAccessedMembersToType (in ReflectionMarker reflectionMarker
234
232
continue ;
235
233
236
234
var interfaceAnnotation = GetCachedInfoForTypeInHierarchy ( interfaceType ) ;
237
- if ( interfaceAnnotation . applied && interfaceAnnotation . annotation . HasFlag ( annotationToApplyToInterfaces ) )
238
- continue ;
239
-
240
- // Apply All or Interfaces to the interface type.
241
- // DAMT.All may produce redundant warnings from implementing types, when the interface type already had some annotations.
242
- reflectionMarker . MarkTypeForDynamicallyAccessedMembers ( origin , interfaceType , annotationToApplyToInterfaces , DependencyKind . DynamicallyAccessedMemberOnType , declaredOnly : false ) ;
235
+ if ( interfaceAnnotation . annotation . HasFlag ( annotationToApplyToInterfaces ) ) {
236
+ if ( ! interfaceAnnotation . applied )
237
+ ApplyDynamicallyAccessedMembersToType ( interfaceType , interfaceAnnotation . annotation ) ;
238
+ } else {
239
+ // Apply All or Interfaces to the interface type.
240
+ // DAMT.All may produce redundant warnings from implementing types, when the interface type already had some annotations.
241
+ _reflectionMarker . MarkTypeForDynamicallyAccessedMembers ( origin , interfaceType , annotationToApplyToInterfaces , DependencyKind . DynamicallyAccessedMemberOnType , declaredOnly : false ) ;
242
+ }
243
243
}
244
244
}
245
245
246
246
// The annotations this type inherited from its base types or interfaces should not produce
247
247
// warnings on the respective base/interface members, since those are already covered by applying
248
248
// the annotations to those types. So we only need to handle the members directly declared on this type.
249
- reflectionMarker . MarkTypeForDynamicallyAccessedMembers ( origin , type , annotation , DependencyKind . DynamicallyAccessedMemberOnType , declaredOnly : true ) ;
249
+ _reflectionMarker . MarkTypeForDynamicallyAccessedMembers ( origin , type , annotation , DependencyKind . DynamicallyAccessedMemberOnType , declaredOnly : true ) ;
250
250
}
251
251
252
252
( DynamicallyAccessedMemberTypes annotation , bool applied ) GetCachedInfoForTypeInHierarchy ( TypeDefinition type )
0 commit comments