[API Proposal]: extend 'Attribute' model for feature APIs for custom feature switches

Open
#112,969 3 comments 1 reaction 1 assignee View on GitHub

@sbomer is already working on this.

Since Mar 5, 2025.

Assessment

This issue has not been assessed yet.

Description

api-suggestion area-Tools-ILLink code-analyzer partner-impact
Background and motivation

This proposal is a follow up to #96859, with the goal being to implement the ability to get analyzer support for custom feature switches. This was initially discussed there as well, but was then removed from the initial proposal. We have several scenarios where this would be great to have:

  • CsWinRT: we have several feature switches available, and right now there is no way to easily understand how they affect the code execution at runtime. This makes it less intuitive for customers to use them, and it also makes it more difficult for us to maintain the codebase, as we have to "just know" what code paths will fail or not depending on combinations of feature switches.
  • Windows feature flags: as we're continuing to adopt C# for OS components, we also implemented a C# backend to check feature flags at runtime. This is extremely widespread in all C# components in Windows, but right now it has no analyzer support whatsoever, making things more brittle and more difficult to maintain. This is another scenario where we could really use support for custom feature flags.
  • MVVM Toolkit: we have a number of feature switches here too, but no way to provide customers with analyzer support so they can easily validate that they're not trying to use code that won't work if they disabled a given feature switch.

The proposal is to make it possible for developers to leverage the same analyzer as for trimming/AOT, but for custom feature switches too.

Prior design discussion: https://github.com/dotnet/designs/pull/305/.

API Proposal
  namespace System.Diagnostics.CodeAnalysis;
  
- [AttributeUsage(AttributeTargets.Property, Inherited = false)]
+ [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, Inherited = false)]
  public sealed class FeatureSwitchDefinitionAttribute : Attribute
  {
      public FeatureSwitchDefinitionAttribute(string switchName);
  
      public string SwitchName { get; }
  }

The role of [FeatureCheck] is to allow 3rd party libraries to define a relationship between a property representing a feature switch, and an attribute they can define to annotate members that rely on that custom feature switch, such that the analyzer can correctly warn (or not) upon usage.

API Usage

The goal is to make this scenario work as follows:

[FeatureSwitchDefinition("MyCustomFeature")]
public static bool IsMyCustomFeatureSupported { get; } = AppContext.TryGetSwitch("MyCustomFeature", out bool flag) ? flag : true;

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Class, Inherited = false)]
[FeatureSwitchDefinition("MyCustomFeature")]
public sealed class RequiresMyCustomFeatureAttribute : Attribute;

class Program
{
    public void Test()
    {
        M(); // Warns
    
        if (IsMyCustomFeatureSupported)
        {
            M(); // Does not warn
        }
    }
    
    [RequiresMyCustomFeature]
    private void M()
    {
    }
}
Risks

No risk. The current situation is strictly worse, as there's no way for 3rd party authors to easily validate feature switch dependent scenarios.

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.