Sefrone.Api.Auth.Generator 3.2.6

Sefrone dotnet api packages

RequiredIfEnumValueAnalyzer Usage Example

This example demonstrates how to use the custom RequiredIfEnumValueAnalyzer in a .NET project to enforce the rule that a nullable property must be set to a non-null value when an enum array contains a specified value.

Setup

First, define the FeatureSet enum and create a class that uses the [RequiredIfEnumValue] attribute.

1. Define the FeatureSet Enum

public enum FeatureSet
{
    Basic,
    Premium,
    Advanced
}

2. Define the RequiredIfEnumValueAttribute

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class RequiredIfEnumValueAttribute : Attribute
{
    public string EnumPropertyName { get; }
    public string DependentPropertyName { get; }
    public int RequiredEnumValue { get; }

    public RequiredIfEnumValueAttribute(string enumPropertyName, string dependentPropertyName, int requiredEnumValue)
    {
        EnumPropertyName = enumPropertyName;
        DependentPropertyName = dependentPropertyName;
        RequiredEnumValue = requiredEnumValue;
    }
}

3. Apply the [RequiredIfEnumValue] Attribute to a Class

[RequiredIfEnumValue("Features", "Data", (int)FeatureSet.Premium)]
public partial class MyFeatureClass
{
    public FeatureSet[] Features { get; set; }
    public string? Data { get; set; }

    public MyFeatureClass(FeatureSet[] features, string? data)
    {
        Features = features;
        Data = data;
    }
}

Behavior of the Analyzer

The analyzer (RequiredIfEnumValueAnalyzer) will check the following:

  • The MyFeatureClass has the [RequiredIfEnumValue] attribute applied.
  • The Features property is an array of the FeatureSet enum type.
  • The Data property is nullable.
  • It will report a diagnostic if the Data property is nullable, indicating that it should not be null when the Features array contains the value FeatureSet.Premium.

Example Usage in a Project

Correct Usage (No Diagnostic)

public class Program
{
    public static void Main()
    {
        var validInstance = new MyFeatureClass(new[] { FeatureSet.Basic, FeatureSet.Premium }, "Non-null data");
        // No diagnostic error here because Data is set to a non-null value.
    }
}

Incorrect Usage (Triggers a Diagnostic)

public class Program
{
    public static void Main()
    {
        var invalidInstance = new MyFeatureClass(new[] { FeatureSet.Premium }, null);
        // This will trigger a diagnostic error from the analyzer because the Data property is null
        // and the Features array contains FeatureSet.Premium.
    }
}

Diagnostic Output

When the analyzer detects the issue in the "Incorrect Usage" example, it will produce a compile-time error similar to:

Error REQUIRED_NULL_CHECK: Property 'Data' should not be null when 'Features' contains the specified enum value.

The error message helps the developer understand that the Data property needs to be set to a non-null value when the Features array includes FeatureSet.Premium.

How to Add the Analyzer to the Project

  1. Add the Analyzer Project to Your Solution:

    • Create a separate analyzer project for the RequiredIfEnumValueAnalyzer, or use an existing one.
    • Build the analyzer project to produce a .dll file for the analyzer.
  2. Reference the Analyzer in the Target Project:

    • Add a reference to the analyzer .dll in the project where you want to apply the rule.
    • You can do this by using NuGet, directly referencing the .dll, or adding the analyzer as a project reference.
  3. Enable the Analyzer in the Target Project:

    • When you build your project, the custom analyzer will automatically run and produce diagnostics based on your defined rules.

By following these steps, you can enforce compile-time validation rules based on custom conditions, providing immediate feedback to developers when they violate the rules defined by the [RequiredIfEnumValue] attribute.

No packages depend on Sefrone.Api.Auth.Generator.

.NET Standard 2.0

Version Downloads Last updated
3.2.6 0 06/20/2026
3.2.5 4 06/16/2026
3.2.4 2 06/16/2026
3.2.3 239 06/07/2026
3.2.2 18 06/07/2026
3.2.1 3 06/07/2026
3.2.0 14 05/30/2026
3.1.9 4 05/30/2026
3.1.8 1 05/30/2026
3.1.7 8 05/30/2026
3.1.6 4 05/30/2026
3.1.5 6 05/29/2026
3.1.4 1 05/26/2026
3.1.3 1 05/26/2026
3.1.2 1 05/26/2026
3.1.1 478 04/23/2026
3.1.0 624 04/12/2026
3.0.9 28 04/11/2026
3.0.8 31 04/09/2026
3.0.7 33 04/07/2026
3.0.6 9 04/07/2026
3.0.5 53 04/06/2026
3.0.4 25 04/06/2026
3.0.3 3 04/05/2026
3.0.2 3 04/05/2026
3.0.1 195 03/30/2026
3.0.0 62 03/25/2026
2.9.9 42 03/18/2026
2.9.8 75 03/17/2026
2.9.7 103 03/12/2026
2.9.6 10 03/10/2026
2.9.5 6 03/10/2026
2.9.4 97 03/07/2026
2.9.3 10 03/04/2026
2.9.2 325 01/11/2026
2.9.1 6 01/11/2026
2.9.0 488 12/21/2025
2.8.9 219 12/16/2025
2.8.8 20 12/16/2025
2.8.7 65 12/14/2025
2.8.6 56 12/09/2025
2.8.5 46 12/09/2025
2.8.4 8 12/09/2025
2.8.3 10 12/08/2025
2.8.2 17 12/08/2025
2.8.1 73 12/03/2025
2.8.0 75 12/01/2025
2.7.9 36 12/01/2025
2.7.8 12 11/30/2025
2.7.7 28 11/30/2025
2.7.6 7 11/30/2025
2.7.5 5 11/30/2025
2.7.4 85 11/23/2025
2.7.3 14 11/23/2025
2.7.2 15 11/23/2025
2.7.1 22 11/22/2025
2.7.0 89 11/18/2025
2.6.9 25 11/15/2025
2.6.8 17 11/13/2025
2.6.7 8 11/13/2025
2.6.6 8 11/13/2025
2.6.5 9 11/12/2025
2.6.4 9 11/12/2025
2.6.3 154 10/19/2025
2.6.2 1,136 10/14/2025
2.6.1 12 10/12/2025
2.6.0 48 10/07/2025
2.5.9 117 10/05/2025
2.5.8 59 10/01/2025
2.5.7 305 09/21/2025
2.5.6 52 09/13/2025
2.5.5 276 08/26/2025
2.5.4 30 08/24/2025
2.5.3 35 08/24/2025
2.5.2 24 08/17/2025
2.5.1 105 08/09/2025
2.5.0 16 08/09/2025
2.4.9 34 07/14/2025
2.4.8 223 07/05/2025
2.4.7 407 06/30/2025
2.4.6 152 06/22/2025
2.4.5 164 06/09/2025
2.4.4 53 06/08/2025
2.4.3 13 06/07/2025
2.4.2 16 06/03/2025
2.4.1 108 06/02/2025
2.4.0 15 06/01/2025
2.3.9 227 05/12/2025
2.3.8 26 05/10/2025
2.3.7 22 05/10/2025
2.3.6 21 05/10/2025
2.3.5 19 05/10/2025
2.3.4 29 05/08/2025
2.3.3 22 05/06/2025
2.3.2 19 05/06/2025
2.3.1 19 05/06/2025
2.3.0 68 05/06/2025
2.2.9 58 05/05/2025
2.2.8 19 05/05/2025
2.2.7 21 05/04/2025
2.2.6 15 05/04/2025
2.2.5 13 05/03/2025
2.2.4 12 04/27/2025
2.2.3 16 04/27/2025
2.2.2 160 04/19/2025
2.2.1 18 04/19/2025
2.2.0 15 04/19/2025
2.1.9 13 04/19/2025
2.1.8 11 04/19/2025
2.1.7 65 04/07/2025
2.1.6 13 04/07/2025
2.1.5 13 04/07/2025
2.1.4 45 04/07/2025
2.1.3 16 04/07/2025
2.1.2 19 04/06/2025
2.1.1 20 03/25/2025
2.1.0 187 03/23/2025
2.0.9 17 03/23/2025
2.0.8 16 03/23/2025
2.0.7 20 03/22/2025
2.0.6 14 03/22/2025
2.0.5 263 02/23/2025
2.0.4 17 02/23/2025
2.0.3 17 02/22/2025
2.0.2 17 02/22/2025
2.0.1 104 02/19/2025
2.0.0 40 02/18/2025
1.9.9 16 02/18/2025
1.9.8 54 02/12/2025
1.9.7 113 02/05/2025
1.9.6 54 02/03/2025
1.9.5 19 02/03/2025
1.9.4 275 01/07/2025
1.9.3 240 12/25/2024
1.9.2 22 12/25/2024
1.9.1 38 12/22/2024
1.9.0 17 12/22/2024
1.8.9 21 12/21/2024
1.8.8 25 12/20/2024
1.8.7 23 12/19/2024
1.8.6 19 12/19/2024
1.8.5 18 12/19/2024
1.8.4 21 12/19/2024
1.8.3 28 12/18/2024
1.8.2 19 12/18/2024
1.8.1 18 12/18/2024
1.8.0 20 12/18/2024
1.7.9 21 12/18/2024
1.7.8 16 12/18/2024
1.7.7 27 12/16/2024
1.7.6 19 12/15/2024
1.7.5 23 12/13/2024
1.7.4 269 12/05/2024
1.7.3 32 11/25/2024
1.7.2 68 11/10/2024
1.7.1 79 10/28/2024
1.7.0 54 10/26/2024
1.6.9 92 10/20/2024
1.6.8 24 10/19/2024
1.6.7 56 09/15/2024
1.6.6 19 09/15/2024
1.6.5 273 09/08/2024
1.6.4 35 09/03/2024
1.6.3 19 09/03/2024
1.6.2 60 08/25/2024
1.6.1 30 08/25/2024
1.6.0 17 08/25/2024
1.5.9 20 08/25/2024
1.5.8 20 08/25/2024
1.5.7 57 08/13/2024
1.5.6 22 08/12/2024
1.5.5 17 08/12/2024
1.5.4 20 08/12/2024
1.5.3 17 08/12/2024
1.5.2 23 08/12/2024
1.5.1 21 08/12/2024
1.5.0 21 08/11/2024
1.4.9 35 07/28/2024
1.4.8 27 07/28/2024
1.4.7 24 07/14/2024
1.4.6 33 06/29/2024
1.4.5 126 06/05/2024
1.4.4 17 06/05/2024
1.4.3 33 06/04/2024
1.4.2 14 06/04/2024
1.4.1 21 06/03/2024
1.4.0 24 06/03/2024
1.3.9 21 06/02/2024
1.3.8 14 06/02/2024
1.3.7 16 06/02/2024
1.3.6 20 06/02/2024
1.3.5 15 06/02/2024
1.3.4 36 06/02/2024
1.3.3 16 06/02/2024
1.3.2 15 06/02/2024
1.3.1 16 06/01/2024
1.3.0 17 06/01/2024
1.2.9 17 06/01/2024
1.2.8 24 06/01/2024
1.2.7 22 05/31/2024
1.2.6 17 05/31/2024
1.2.5 28 05/31/2024
1.2.4 18 05/31/2024
1.2.2 17 05/31/2024
1.2.1 40 05/30/2024