@@ -309,35 +309,32 @@ public static Attribute[] GetAllCustomAttributes<TAttribute>(this PropertyInfo m
309309
310310 public static bool AcceptsNull ( this MemberInfo member )
311311 {
312- var result = true ; //default to allowing nulls, this will be set to false if there is a null context on the type
313312#if NET8_0_OR_GREATER
314- var typeHasNullContext = TypesHaveNullContext . GetOrAdd ( member . DeclaringType , ( Type t ) =>
315- {
316- var attributes = t . GetCustomAttributes ( typeof ( System . Runtime . CompilerServices . NullableContextAttribute ) , true ) ;
317- return ( attributes ? . Length ?? 0 ) > 0 ;
318- } ) ;
313+ var classAttributes = member . DeclaringType . GetCustomAttributes ( typeof ( System . Runtime . CompilerServices . NullableContextAttribute ) , true ) ;
314+ var defaultFlag = classAttributes . OfType < System . Runtime . CompilerServices . NullableContextAttribute > ( ) . FirstOrDefault ( ) ? . Flag ?? 0 ;
319315
320- if ( typeHasNullContext )
321- {
322- // we have a nullable context on that type, only allow null if the NullableAttribute is on the member.
323- var memberAttributes = member . GetCustomAttributes ( typeof ( System . Runtime . CompilerServices . NullableAttribute ) , true ) ;
324- result = ( memberAttributes ? . Length ?? 0 ) > 0 ;
325- }
316+ // we have a nullable context on that type, only allow null if the NullableAttribute is on the member.
317+ var memberAttributes = member . GetCustomAttributes ( typeof ( System . Runtime . CompilerServices . NullableAttribute ) , true ) ;
318+ var nullableFlag = memberAttributes . OfType < System . Runtime . CompilerServices . NullableAttribute > ( ) . FirstOrDefault ( ) ? . NullableFlags . Any ( flag => flag == 2 ) ;
319+ var result = nullableFlag ?? defaultFlag == 2 ;
326320
327321 return result ;
328322#else
329- var typeHasNullContext = TypesHaveNullContext . GetOrAdd ( member . DeclaringType , ( Type t ) =>
323+ var classAttributes = member . DeclaringType . GetCustomAttributes ( true ) ;
324+ var classAttribute = classAttributes . FirstOrDefault ( x => x . GetType ( ) . FullName == "System.Runtime.CompilerServices.NullableContextAttribute" ) ;
325+ var defaultFlag = 0 ;
326+ if ( classAttribute != null )
330327 {
331- var attributes = t . GetCustomAttributes ( true ) ;
332- return attributes . Any ( x => x . GetType ( ) . FullName == "System.Runtime.CompilerServices.NullableContextAttribute" ) ;
333- } ) ;
334-
335- if ( typeHasNullContext )
336- {
337- var memberAttributes = member . GetCustomAttributes ( true ) ;
338- result = memberAttributes . Any ( x => x . GetType ( ) . FullName == "System.Runtime.CompilerServices.NullableAttribute" ) ;
328+ var classAttributeType = classAttribute . GetType ( ) ;
329+ var classProperty = classAttributeType . GetProperty ( "Flag" ) ! ;
330+ defaultFlag = ( byte ) classProperty . GetValue ( classAttribute ) ! ;
339331 }
340-
332+ var memberAttributes = member . GetCustomAttributes ( true ) ;
333+ var memberAttribute = memberAttributes . FirstOrDefault ( x => x . GetType ( ) . FullName == "System.Runtime.CompilerServices.NullableAttribute" ) ;
334+ var memberAttributeType = memberAttribute ? . GetType ( ) ;
335+ var memberProperty = memberAttributeType ? . GetProperty ( "NullableFlags" ) ! ;
336+ var flags = ( byte [ ] ) memberProperty . GetValue ( memberAttribute ) ! ;
337+ var result = flags . Any ( x => x == 2 ) || defaultFlag == 2 ;
341338 return result ;
342339#endif
343340 }
0 commit comments