-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: Reflecting to find nullability of a generic return type is unreasonably complicated #110971
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection |
This isn't specific to index properties. Rather, this is because using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
public class C {
public static void Main() {
NullabilityInfoContext nullabilityInfoContext = new NullabilityInfoContext();
{
Type type = typeof(List<string>);
PropertyInfo property = type.GetProperties().Single(
p => p.GetIndexParameters().Length == 1);
NullabilityInfo info = nullabilityInfoContext.Create(property);
Console.WriteLine($"t={info.Type}, et={info.ElementType}, rs={info.ReadState}, ws={info.WriteState}");
}
{
Type type = typeof(Box1<object>);
PropertyInfo property = type.GetProperty(nameof(Box1<object>.Value))!;
NullabilityInfo info = nullabilityInfoContext.Create(property);
Console.WriteLine($"t={info.Type}, et={info.ElementType}, rs={info.ReadState}, ws={info.WriteState}");
}
{
Type type = typeof(Box2<object>);
PropertyInfo property = type.GetProperty(nameof(Box2<object>.Value))!;
NullabilityInfo info = nullabilityInfoContext.Create(property);
Console.WriteLine($"t={info.Type}, et={info.ElementType}, rs={info.ReadState}, ws={info.WriteState}");
}
}
internal class Box1<T>
{
public Box1(T value)
{
this.Value = value;
}
public T Value { get; set; }
}
internal class Box2<T> where T : notnull
{
public Box2(T value)
{
this.Value = value;
}
public T Value { get; set; }
}
} Output:
|
The TypeWithAnnotations idea mentioned in #29723 (comment) could solve this. You wouldn't be able to get that from Object.GetType() though, because the information doesn't exist there. |
...that sure kicks the legs out from under my project, but thank you for letting me know. |
Closing. Nullability works with generic members (including indexer properties) provided the generic type is a value type (either wrapped or not with For this section:
The "ReturnValue" info can be obtained by using the Is the proposal to add a |
Background and motivation
For my specific problem, I am trying to recursively walk each member of an object to see if a non-nullable type has had null assigned.
E.g.
Which has been a blast to write, until I encountered Index Properties - specifically generic index properties, which always claim to be allowed to return null.
Quite probably I'm just missing something, but I'm missing something because this API is currently less than intuitive.
First, I'd love to replace this:
with:
Once I have the index property, next step looks like:
Which only works for non-generic index properties. Try this on e.g. a
var l = new List<string>()
and it'll happily say thatl[2]
can return null.API Proposal
I'm hoping for two things here
NullabilityInfoContext.Create(PropertyInfo)
expanded to also assess nullability of generic index propertiesNullabilityInfo
for methods (and property methods) that lets you query nullability in slightly more natural language.API Usage
here
info
with additional members usable like:Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: