-
Notifications
You must be signed in to change notification settings - Fork 5k
NullabilityInfoContext returns incorrect NullabilityInfo values for Generics #115014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The desired result is impossible to get through reflection from the generic type itself unless Note that If you need this at runtime without doing source code analysis/Roslyn, as far as reference types as generic arguments are concerned, one approach is finding some field/property/method that is using the type So, if you for example have some method somewhere like: public class Bar
{
public MyGenericClass<string> Foo() { ... }
} then you can query the return parameter (not the generic type!) to see whether it is a generic type with nullable/non-nullable generic arguments. E.g.: var returnParameter = typeof(Bar).GetMethod("Foo", BindingFlags.Public | BindingFlags.Instance).ReturnParameter;
NullabilityInfo ni = new NullabilityInfoContext().Create(returnParameter);
Console.WriteLine(ni.GenericTypeArguments[0].ReadState); |
Tagging subscribers to this area: @dotnet/area-system-reflection |
That explains everything I am seeing. 🙂👍 Unfortunate since I'm in a Thank you @elgonzo for the quick and very clear response! |
Description
When using the
NullabilityInfoContext
to fetchNullabilityInfo
for a generic property, the ReadState and WriteState are always Nullable, even when the defined generic has the type as NotNull.Reproduction Steps
Expected behavior
The resulting NullabilityInfo's
ReadState
andWriteState
should match the implemented generic type.string == NotNull
.string? == Nullable
Actual behavior
When genaric
T
is a reference type,NullablityInfo
has the ReadStateNullable
.Regression?
No response
Known Workarounds
Not using generics works fine (see below). But it's not really a work around.
Configuration
Dotnet 8.0
Nullable enabled
Other information
More Expected/Actual
The text was updated successfully, but these errors were encountered: