@@ -23,63 +23,12 @@ public static bool HasGenericParameterNotNullConstraint(this Type genericParamet
2323 {
2424 ValidateGenericParameterArgument ( genericParameter , nameof ( genericParameter ) ) ;
2525
26- var attrNullable = genericParameter . CustomAttributes . FirstOrDefault ( IsNullableAttribute ) ;
27- var attrNullableContext = FindNullableContextAttribute ( genericParameter ) ;
28-
29- const byte notAnnotated = 1 ;
30-
31- if ( attrNullableContext is not null && notAnnotated . Equals ( attrNullableContext . ConstructorArguments [ 0 ] . Value ) )
26+ if ( genericParameter . GetNullableContextAttributeMetadataValue ( ) == NullableMetadataValue . NotAnnotated )
3227 // `#nullable enable` context
33- return attrNullable is null ;
28+ return genericParameter . GetNullableAttributeMetadataValue ( ) == null ;
3429 else
3530 // `#nullable disable` context
36- return attrNullable is not null && notAnnotated . Equals ( attrNullable . ConstructorArguments [ 0 ] . Value ) ;
37- }
38-
39- private static bool IsNullableAttribute ( CustomAttributeData attr )
40- => "System.Runtime.CompilerServices.NullableAttribute" . Equals ( attr . AttributeType . FullName , StringComparison . Ordinal ) ;
41-
42- private static bool IsNullableContextAttribute ( CustomAttributeData attr )
43- => "System.Runtime.CompilerServices.NullableContextAttribute" . Equals ( attr . AttributeType . FullName , StringComparison . Ordinal ) ;
44-
45- private static bool TryGetNullableContextAttribute (
46- MemberInfo member ,
47- #if NULL_STATE_STATIC_ANALYSIS_ATTRIBUTES
48- [ NotNullWhen ( true ) ]
49- #endif
50- out CustomAttributeData ? attrNullableContext
51- )
52- {
53- attrNullableContext = member . CustomAttributes . FirstOrDefault ( IsNullableContextAttribute ) ;
54-
55- return attrNullableContext is not null ;
56- }
57-
58- private static CustomAttributeData ? FindNullableContextAttribute ( Type genericParameter )
59- {
60- if (
61- #if SYSTEM_TYPE_ISGENERICMETHODPARAMETER
62- genericParameter . IsGenericMethodParameter &&
63- #else
64- genericParameter . DeclaringMethod is not null &&
65- genericParameter . IsGenericParameter &&
66- #endif
67- TryGetNullableContextAttribute ( genericParameter . DeclaringMethod ! , out var methodAttr )
68- ) {
69- return methodAttr;
70- }
71-
72- Type? t = genericParameter;
73-
74- for ( ; ; ) {
75- if ( ( t = t ! . DeclaringType ) is null )
76- return null ;
77- if ( TryGetNullableContextAttribute ( t , out var typeAttr ) )
78- return typeAttr;
79-
80- // retry against to the outer type
81- continue ;
82- }
31+ return genericParameter . GetNullableAttributeMetadataValue ( ) == NullableMetadataValue . NotAnnotated ;
8332 }
8433
8534 public static bool HasGenericParameterNoConstraints ( this Type genericParameter )
0 commit comments