You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NullabilityInfoContext reporting incorrect nullability for ParameterInfo of generic types #92487
When it comes to generic types, it is not possible for the NullabilityInfoContext class to determine the nullability of members whose type is a generic parameter -- in such cases NullabilityInfoContext will fall back to reporting the members as being Nullable. This works fine for the cases of PropertyInfo and FieldInfo, however I found a case where the nullability of a ParameterInfo is reported erroneously as NotNull.
Reproduction Steps
Typetype=typeof(MyPoco<string>);varctx=newNullabilityInfoContext();// Property nullability -- works as expectedPropertyInfoprop=type.GetProperties()[0];NullabilityInfopropNullability=ctx.Create(prop);Console.WriteLine(propNullability.ReadState);// Nullable// Parameter nullability -- ReadState & WriteState reported as NotNullvarparam=type.GetConstructors()[0].GetParameters()[0];varinfo=ctx.Create(param);Console.WriteLine(info.ReadState);// NotNullConsole.WriteLine(info.WriteState);// NotNullpublicclassMyPoco<T>{publicMyPoco(Tvalue)=>Value=value;publicTValue{get;}}
Expected behavior
Parameter nullability should be reported as Nullable, similar to the property.
Actual behavior
Reported as NotNull.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
Adding a notnull constraint makes nullability information being reported as expected:
Activity
eiriktsarpalis commentedon Sep 22, 2023
Related to #1256
karakasa commentedon Sep 23, 2023
It's likely because
NullabilityInfoContext.CheckParameterMetadataTypedoesn't handle.ctorruntime/src/libraries/System.Private.CoreLib/src/System/Reflection/NullabilityInfoContext.cs
Line 87 in b3ef498
I have implemented a quick fix. Let me check if it works.
Improve nullability check for generic parameters in ctor
Improve nullability check for generic `.ctor` parameters (#92514)
Sync code (#1)