Skip to content

[API Proposal]: Expose the NullabilityInfoContext.IsSupported property #103004

Description

@eiriktsarpalis

Background and motivation

NullabilityInfoContext.IsSupported is an internal property pointing to the System.Reflection.NullabilityInfoContext.IsSupported feature switch switch that additionally defines a corresponding ILLink substitution. If explicitly turned off, this causes the NullabilityInfoContext class to throw exceptions, which can create problems with third-party components that depend on NullabilityInfoContext. 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

NullabilityInfo? info = NullabilityInfoContext.IsSupported
    ? new NullabilityInfoContext().Create(propertyInfo)
    : null;

Alternative Designs

The implementation of NullabilityInfoContext could be updated so that exceptions are no longer being thrown.

Risks

No response

Metadata

Metadata

Labels

api-ready-for-reviewAPI is ready for review, it is NOT ready for implementationarea-System.ReflectionblockingMarks issues that we want to fast track in order to unblock other important work

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions