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
Background and motivation
NullabilityInfoContext.IsSupportedis an internal property pointing to theSystem.Reflection.NullabilityInfoContext.IsSupportedfeature switch switch that additionally defines a corresponding ILLink substitution. If explicitly turned off, this causes theNullabilityInfoContextclass 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
NullabilityInfoContextcould be updated so that exceptions are no longer being thrown.Risks
No response