5

In other words, is there an Attribute that marks a segment of code as not too old, but too new and therefore not quite ready for widespread use?

If I'd have to create a custom Attribute to accomplish this, that's fine. I just wanted to make sure first.

William
  • 443
  • 4
  • 15

3 Answers3

4

No, there's nothing standardized around this. You might want to consider just not exposing code like that though - or only exposing it in beta builds etc.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Not an attribute, but there are preprocessor directives (https://msdn.microsoft.com/en-us/library/ed8yd1ha.aspx) which we can use to mark a region of code as "too new" to run. Basically you can define a flag to indicate that piece of code is ready.

Here is an example:

#define FOREST_CAN_RUN
//undef FOREST_CAN_RUN --> disable that feature
using System;
namespace Test
{
    public class Forest
    {
        public void Run()
        {
#if FOREST_CAN_RUN
            Console.Write("Run Forest, Run !");
#else
            Console.Write("Sorry, Jenny");
#endif
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            Forest f= new Forest ();
            f.Run();
        }
    }
}
Dio Phung
  • 5,944
  • 5
  • 37
  • 55
0

Since .NET 6 there is RequiresPreviewFeaturesAttribute which can be used for this goal. Actually it was used for generic math preview in .NET 6. From Preview Features in .NET 6 – Generic Math:

Central to everything else is the new RequiresPreviewFeatures attribute and corresponding analyzer. This attribute allows us to annotate new preview types and new preview members on existing types. With this capability, we can ship an unsupported preview feature inside a supported major release.

Note that at the moment it is used by .NET team and does not have a way to distinguish between features (it either enables all of them or disables) but in theory it should not be that hard to roll out a custom one with custom Roslyn analyzer which will be able to distinguish between the features (for example using similar approach - adding to the .csproj corresponding custom section with child collection of named features).

Guru Stron
  • 102,774
  • 10
  • 95
  • 132