-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Reflect nullability with new C# 8 feature #9662
Comments
Thanks for bringing this to our attention @RSuter It's a very good point. Preview 1 is a bit early for this content, as some details are still being discussed and subject to change. I've tagged it for the .NET Core 3 release, and added a milestone to make sure we cover this in one of the later previews. |
Is there already some discussions, description, info about that? Can you provide some links e.g. to GitHub issues? |
It should be possible to figure this out via reflection, but not trivial. The Nullable attribute is generated so that it stores the relevant flags, so that the flags are accessible via reflection. I can also dig up the section of our tests that cover this, if that helps. I'm curious to understand the scenarios where this is useful to get this information at runtime via reflection. Can you share some more details? |
Specifically it would be needed here: (The type parameter is coming from a property or parameter info) A simple way would be to add support for the NullableAttribute there and set DefaultReferenceTypeNullHandling to NotNull but then it would be global for all assemblies and not check your context-based feature opt-in attributes |
I’m pretty sure there are other scenarios, e.g. assembly based documentation generators (docfx?), etc. Ideally there would be a .net standard 1.0 package with MemberInfo extensions to get the nullability info (it’s a compiler feature which works in all .net frameworks, right?) - one can only hope 🙂 |
Tagging @terrajobst @danmosemsft @jaredpar. |
No, but I recently had the same thought. Historically, reflection APIs haven't been updated to reflect "common" conventions, such as extension methods, or dynamic. It pretty much exposes more or less raw metadata. This is especially painful for things like nullable and tuple names where getting the correct information from metadata is rocket science. However, we should do a diff how Roslyn symbol APIs exposes these concepts and come up with a proposal for reflection. /cc @steveharter. |
That's why it should not be implemented by external libraries - mainly to avoid implementing it multple times (once per lib), ensure it's behaving correctly and the stuff just works even if there are internal changes... And please don't just add this to .net standard 2.1 as this would mean that the API is only available in the latest runtimes and users using C# 8 in older project are left out... And yes, potentially the same applies for tuple names as this information might be useful to generate correct JSON Schemas (I don't have a request for supporting this [yet]). |
@jcouv can you point me at the code where this is already implemented? |
I'm moving this milestone out a bit. We shouldn't write the docs until we have a clear recommendation. |
@RSuter some of the complication is due to the compiler generated attribute not having a field / property where the flag is set. That forced the code to do constructor argument parsing which isn't pretty. As of dev16.u1p1 there is a field now that consumers can read. Simplifies that a bit. |
Ref: This new reflection api might also be used by the EF Core project: dotnet/efcore#10347 |
Putting this in the backlog, because I don't think the story is set correctly yet. |
I implemented my own lib for nullability reflection: https://github.com/RicoSuter/Namotion.Reflection/blob/master/README.md#nullability-reflection-c-8 Now used in NSwag and NJS |
This issue has been closed as part of the issue backlog grooming process outlined in #22351. That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the |
How can we reflect over the nullability of props, params etc e.g. for
string
vsstring?
with the new C# 8 nullability output? Is there a new api for that?See https://blog.rsuter.com/the-output-of-nullable-reference-types-and-how-to-reflect-it/
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: