-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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]: Expose the NullabilityInfoContext.IsSupported
property
#103004
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection |
We don't document anything around this. The I think the whole existence of Cc @eerhardt |
Would this always return
The S.T.J source generator still work even without And out of curiosity, given that this is a trimming option that utilizes ILLink substitution, is there a realistic scenario where an application built against an older SDK that did support |
I'll add a bit background. In #39000 we started to unconditionally trim all custom attributes that looked "unnecessary" on WASM. This included all the nullable attributes. This was an illegal optimization because it caused a behavioral difference that is only observable after trimming, without raising a publish-time warning (we don't annotate custom attribute APIs as trimming-unfriendly and make no effort to analyze what attribute types are reflected on). The justification at that time was that "few people will be broken". Soon after that, Now System.Text.Json started using I think we're at a fork where we can do one of these:
Cc @dotnet/illink-contrib |
I'd be OK with removing the feature switch all together if the affected app model teams agree. I believe Blazor WASM is the only one using the switch. In my measuring of a .NET 8 Blazor WASM app, it adds 0.76% of size to the brotli compressed app:
|
Another problem with the existing I agree that it would be best to delete the existing |
Do we need API review to remove a feature switch? Seems we might as well just yank it. |
Another question is how we should be handling older runtimes that include |
It is possible. You can do try/catch around NullabilityInfoContext in the down-level packages and rethrow the exception with a better error message that says the new package System.Text.Json is not compatible with NullabilityInfoContextSupport disabled and tell people how to enable it. |
It is also used in mobile https://github.com/search?q=org%3Axamarin%20NullabilityInfoContextSupport&type=code but I don't think that changes the arguments for or against materially. |
Manually reading the feature switch is actually supposed to work on wasm too, but we haven't had time to diagnose why the required build step isn't working in blazor at the moment and it wasn't noticed because the substitution was still working. |
namespace System.Reflection;
public partial class NullabilityInfoContext
{
public static bool IsSupported { get; } = AppContext.TryGetSwitch("System.Reflection.NullabilityInfoContext.IsSupported", out bool isSupported) ? isSupported : true;
} |
Background and motivation
NullabilityInfoContext.IsSupported
is an internal property pointing to theSystem.Reflection.NullabilityInfoContext.IsSupported
feature switch switch that additionally defines a corresponding ILLink substitution. If explicitly turned off, this causes theNullabilityInfoContext
class to throw exceptions, which can create problems with third-party components that depend onNullabilityInfoContext
. This is particularly problematic in SDKs that turn off this feature by default (such as Blazor wasm).STJ attempted to address this problem by manually reading the feature switch, however this effort was impeded by the fact that feature switches cannot currently be read at run-time by Blazor wasm. Instead, Blazor wasm relies on ILLink substitutions for this configuration to be read. We should make this property public so that third-party applications can determine if they can use
NullabilityInfoContext
.cc @javiercn
API Proposal
namespace System.Reflection; public class NullabilityInfoContext { + public static bool IsSupported { get; } = AppContext.TryGetSwitch("System.Reflection.NullabilityInfoContext.IsSupported", out bool isSupported) ? isSupported : true; }
API Usage
Alternative Designs
The implementation of
NullabilityInfoContext
could be updated so that exceptions are no longer being thrown.Risks
No response
The text was updated successfully, but these errors were encountered: